diff --git a/.gitignore b/.gitignore index 567609b..a007fea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -build/ +build/* diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f7739d..335eeba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -# Version Check +# Version check cmake_minimum_required(VERSION 3.11) # Default CXX Standard @@ -13,20 +13,18 @@ if (NOT EXISTS ${CMAKE_BINARY_DIR}/CMakeCache.txt) endif() endif() +set(cairo-wrap "cairo_wrap") + # Project project(cairo-wrap) -# Add Files +# Add files file(GLOB sources "*.cpp") -# Add Executable -add_library(cairo_wrap STATIC ${sources}) +# Add library +add_library(${cairo-wrap} STATIC ${sources}) -target_link_libraries(cairo_wrap -lcairo) +target_link_libraries(${cairo-wrap} cairo) # Compiler Options -set(DEBUG_OPTIONS -fexecptions -g -Wall -pedantic-errors -pedantic) -target_compile_options(cairo_wrap PUBLIC "$<$:${DEBUG_OPTIONS}>") - -set(RELEASE_OPTIONS -fexceptions -O3 -Wall -pedantic) -target_compile_options(cairo_wrap PUBLIC "$<$:${RELEASE_OPTIONS}>") \ No newline at end of file +target_compile_options(${cairo-wrap} PUBLIC -fexceptions -std=c++14 -O3 -Wall -pedantic-errors -pedantic) diff --git a/arrow.cpp b/arrow.cpp new file mode 100644 index 0000000..d06d30e --- /dev/null +++ b/arrow.cpp @@ -0,0 +1,40 @@ +#include "arrow.hpp" + +#include + +#ifndef M_PI +# define M_PI 3.14159265358979323846264338327950288 +#endif + +namespace Cairo +{ +//##################################################################################################################### + Arrow::Arrow(DrawContext* ctx, double x, double y, double x2, double y2, double arrowDegree, double arrowLength) + : Line{ctx, x, y, x2, y2} + , arrowDegree_{arrowDegree} + , arrowLength_{arrowLength} + { + + } +//--------------------------------------------------------------------------------------------------------------------- + void Arrow::draw(Pen const& line, Pen const& fill) + { + Line::draw(line); + + double angle = atan2 (y2_ - y_, x2_ - x_) + M_PI; + + auto x1 = x2_ + arrowLength_ * cos(angle - arrowDegree_); + auto y1 = y2_ + arrowLength_ * sin(angle - arrowDegree_); + auto x2 = x2_ + arrowLength_ * cos(angle + arrowDegree_); + auto y2 = y2_ + arrowLength_ * sin(angle + arrowDegree_); + + cairo_move_to(*ctx_, x2_, y2_); + cairo_line_to(*ctx_, x1, y1); + stroke(ctx_, fill); + + cairo_move_to(*ctx_, x2_, y2_); + cairo_line_to(*ctx_, x2, y2); + stroke(ctx_, fill); + } +//##################################################################################################################### +} diff --git a/arrow.hpp b/arrow.hpp new file mode 100644 index 0000000..b03f66e --- /dev/null +++ b/arrow.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "line.hpp" + +namespace Cairo +{ + class Arrow : public Line + { + public: + Arrow(DrawContext* ctx, double x, double y, double x2, double y2, double arrowDegree = 0.5, double arrowLength = 8); + + void draw(Pen const& line, Pen const& fill = {}) override; + + protected: + double arrowDegree_; + double arrowLength_; + }; +} diff --git a/draw_context.cpp b/draw_context.cpp index a1fb4d5..66b7b27 100644 --- a/draw_context.cpp +++ b/draw_context.cpp @@ -13,11 +13,21 @@ namespace Cairo } void DrawContext::fill(color_space r, color_space g, color_space b, color_space a) { - save(); - cairo_set_source_rgba (ctx_, r, g, b, a); + save(); + cairo_set_source_rgba( + ctx_, + static_cast (r) / colorMax, + static_cast (g) / colorMax, + static_cast (b) / colorMax, + static_cast (a) / colorMax + ); cairo_set_operator(ctx_, CAIRO_OPERATOR_SOURCE); cairo_paint(ctx_); restore(); + } + void DrawContext::fill(RGBA color) + { + fill(color.r, color.g, color.b, color.a); } void DrawContext::save() { diff --git a/draw_context.hpp b/draw_context.hpp index ead32d3..85e5715 100644 --- a/draw_context.hpp +++ b/draw_context.hpp @@ -2,7 +2,7 @@ #include "core.hpp" #include "surface.hpp" - +#include "color.hpp" namespace Cairo { @@ -16,7 +16,8 @@ namespace Cairo return ctx_; } - void fill(color_space r, color_space g, color_space b, color_space a = 0xFF); + void fill(color_space r, color_space g, color_space b, color_space a = 0xFF); + void fill(RGBA color); void save(); void restore(); diff --git a/line.hpp b/line.hpp index 8a700d8..c504542 100644 --- a/line.hpp +++ b/line.hpp @@ -11,7 +11,7 @@ namespace Cairo void draw(Pen const& line, Pen const& fill = {}) override; - private: + protected: double x2_; double y2_; }; diff --git a/path.cpp b/path.cpp new file mode 100644 index 0000000..4fa04e3 --- /dev/null +++ b/path.cpp @@ -0,0 +1,26 @@ +#include "path.hpp" + +namespace Cairo +{ + Path::Path(DrawContext* ctx, double x, double y) + : Shape(ctx, x, y) + { + + } + void Path::start() + { + cairo_new_sub_path(*ctx_); + } + void Path::lineTo(double x, double y) + { + cairo_line_to(*ctx_, x, y); + } + void Path::finish() + { + cairo_close_path(*ctx_); + } + void Path::draw(Pen const& line, Pen const& fill) + { + Cairo::stroke(ctx_, line); + } +} \ No newline at end of file diff --git a/path.hpp b/path.hpp new file mode 100644 index 0000000..1ea2fe8 --- /dev/null +++ b/path.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "shape.hpp" + +namespace Cairo +{ + class Path : public Shape + { + public: + Path(DrawContext* ctx, double x = 0., double y = 0.); + + void start(); + + void lineTo(double x, double y); + + void finish(); + + void draw(Pen const& line, Pen const& fill); + }; + +} \ No newline at end of file diff --git a/pen.cpp b/pen.cpp index b4681f8..88ae898 100644 --- a/pen.cpp +++ b/pen.cpp @@ -12,7 +12,13 @@ namespace Cairo void applyPen(DrawContext* ctx, Pen const& pen) { cairo_set_line_width(*ctx, pen.width); - cairo_set_source_rgba(*ctx, pen.color.r, pen.color.g, pen.color.b, pen.color.a); + cairo_set_source_rgba( + *ctx, + static_cast (pen.color.r) / colorMax, + static_cast (pen.color.g) / colorMax, + static_cast (pen.color.b) / colorMax, + static_cast (pen.color.a) / colorMax + ); } //--------------------------------------------------------------------------------------------------------------------- void stroke(DrawContext* ctx, Pen const& pen, bool preserve) diff --git a/round_rectangle.cpp b/round_rectangle.cpp index 3a98408..6c2962a 100644 --- a/round_rectangle.cpp +++ b/round_rectangle.cpp @@ -2,6 +2,10 @@ #include +#ifndef M_PI +# define M_PI 3.14159265358979323846264338327950288 +#endif + namespace Cairo { RoundRectangle::RoundRectangle(DrawContext* ctx, double x, double y, double width, double height, double radius) diff --git a/shapes.hpp b/shapes.hpp index d97a8dd..1f4188f 100644 --- a/shapes.hpp +++ b/shapes.hpp @@ -5,3 +5,4 @@ #include "line.hpp" #include "text.hpp" #include "arc.hpp" +#include "path.hpp" diff --git a/surface.cpp b/surface.cpp index b21aa27..dbe8814 100644 --- a/surface.cpp +++ b/surface.cpp @@ -11,7 +11,7 @@ namespace Cairo { cairo_surface_destroy(surface_); } - void Surface::saveToFile(std::string const& fileName) + void Surface::saveToFile(std::string const& fileName) const { cairo_surface_write_to_png(surface_, fileName.c_str()); } diff --git a/surface.hpp b/surface.hpp index 064a451..1221001 100644 --- a/surface.hpp +++ b/surface.hpp @@ -16,7 +16,7 @@ namespace Cairo return surface_; } - void saveToFile(std::string const& fileName); + void saveToFile(std::string const& fileName) const; private: cairo_surface_t* surface_; diff --git a/text.cpp b/text.cpp index 1d31801..2301da2 100644 --- a/text.cpp +++ b/text.cpp @@ -41,6 +41,21 @@ namespace Cairo return box; } +//--------------------------------------------------------------------------------------------------------------------- + double Text::getBaselineDisplacement(Pen const& line) const + { + ctx_->save(); + cairo_select_font_face(*ctx_, font_.family.c_str(), font_.slant, font_.weight); + cairo_set_font_size(*ctx_, font_.size); + applyPen(ctx_, line); + cairo_text_extents_t te; + cairo_font_extents_t fe; + cairo_text_extents(*ctx_, text_.c_str(), &te); + cairo_font_extents (*ctx_, &fe); + ctx_->restore(); + + return (y_ - te.y_bearing + te.height) - (y_ - te.y_bearing) + te.y_bearing; + } //--------------------------------------------------------------------------------------------------------------------- void Text::setFont(Font const& font) { diff --git a/text.hpp b/text.hpp index 602256b..7781c7d 100644 --- a/text.hpp +++ b/text.hpp @@ -12,6 +12,7 @@ namespace Cairo Text(DrawContext* ctx, double x, double y, std::string const& text, Font const& font); void draw(Pen const& line, Pen const& fill = {}) override; BoundingBox calculateBounds(Pen const& line) const; + double getBaselineDisplacement(Pen const& line) const; void setFont(Font const& font); Font& getFont();