From dbc4921f73f3b02919fadb4d8a3e9d876a194365 Mon Sep 17 00:00:00 2001 From: 5cript Date: Wed, 24 May 2017 16:40:41 +0200 Subject: [PATCH 1/9] Added baseline displacement calculation. --- text.cpp | 15 +++++++++++++++ text.hpp | 1 + 2 files changed, 16 insertions(+) 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(); From 79f9d153ee707ad12a410f69099e20e9aed34ec6 Mon Sep 17 00:00:00 2001 From: 5cript Date: Mon, 20 Nov 2017 19:18:08 +0100 Subject: [PATCH 2/9] Added paths. --- CMakeLists.txt | 18 ++++++++++++++++++ path.cpp | 26 ++++++++++++++++++++++++++ path.hpp | 21 +++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 path.cpp create mode 100644 path.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ff4a8cc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +# Version check +cmake_minimum_required (VERSION 3.0) + +set(cairo-wrap "cairo_wrap") + +# Project +project(cairo-wrap) + +# Add files +file(GLOB sources "*.cpp") + +# Add library +add_library(${cairo-wrap} STATIC ${sources}) + +target_link_libraries(${cairo-wrap} cairo) + +# Compiler Options +target_compile_options(${cairo-wrap} PUBLIC -fexceptions -std=c++14 -O3 -Wall -pedantic-errors -pedantic) \ No newline at end of file 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 From cb5fbaf45f3a69498e59a56dc0a45d8f8437de1a Mon Sep 17 00:00:00 2001 From: 5cript Date: Mon, 20 Nov 2017 19:18:54 +0100 Subject: [PATCH 3/9] Fixes and added path to shapes. --- round_rectangle.cpp | 2 ++ shapes.hpp | 1 + 2 files changed, 3 insertions(+) diff --git a/round_rectangle.cpp b/round_rectangle.cpp index 3a98408..f7fdb28 100644 --- a/round_rectangle.cpp +++ b/round_rectangle.cpp @@ -2,6 +2,8 @@ #include +#define M_PI 3.14159265358979323846264338327950288 + 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" From 65549cd8e4d6b3f4711a20b9ffa884f8cb993887 Mon Sep 17 00:00:00 2001 From: 5cript Date: Tue, 21 Nov 2017 18:12:10 +0100 Subject: [PATCH 4/9] Added arrows. --- arrow.cpp | 38 ++++++++++++++++++++++++++++++++++++++ arrow.hpp | 18 ++++++++++++++++++ draw_context.cpp | 8 ++++++++ draw_context.hpp | 5 +++-- line.hpp | 2 +- 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 arrow.cpp create mode 100644 arrow.hpp diff --git a/arrow.cpp b/arrow.cpp new file mode 100644 index 0000000..ba25ba0 --- /dev/null +++ b/arrow.cpp @@ -0,0 +1,38 @@ +#include "arrow.hpp" + +#include + +#define M_PI 3.14159265358979323846264338327950288 + +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..d91f540 100644 --- a/draw_context.cpp +++ b/draw_context.cpp @@ -18,6 +18,14 @@ namespace Cairo cairo_set_operator(ctx_, CAIRO_OPERATOR_SOURCE); cairo_paint(ctx_); restore(); + } + void DrawContext::fill(RGBA color) + { + save(); + cairo_set_source_rgba (ctx_, color.r, color.g, color.b, color.a); + cairo_set_operator(ctx_, CAIRO_OPERATOR_SOURCE); + cairo_paint(ctx_); + restore(); } 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_; }; From e83517e84fcaae3d04f13a995aa2aa3d9e108b2b Mon Sep 17 00:00:00 2001 From: 5cript Date: Thu, 23 Nov 2017 19:11:16 +0100 Subject: [PATCH 5/9] Added missing const. --- surface.cpp | 2 +- surface.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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_; From 642c00fb4b33fe3e31fcd15414fbe4f498cbe6dd Mon Sep 17 00:00:00 2001 From: 5cript Date: Wed, 31 Jan 2018 18:51:55 +0100 Subject: [PATCH 6/9] Added ignore file. --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07ed706 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/* \ No newline at end of file From dfba745f6a4006c29a6cf9dc814f99254581f773 Mon Sep 17 00:00:00 2001 From: 5cript Date: Wed, 3 Jul 2019 19:29:30 +0200 Subject: [PATCH 7/9] Fixed simple bug with colors, weird it never came up??? --- pen.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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) From 13b47c55423c7282469968604ade2c07284a3f46 Mon Sep 17 00:00:00 2001 From: 5cript Date: Tue, 9 Jul 2019 17:37:32 +0200 Subject: [PATCH 8/9] Fixed wrong color for filling. --- draw_context.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/draw_context.cpp b/draw_context.cpp index d91f540..66b7b27 100644 --- a/draw_context.cpp +++ b/draw_context.cpp @@ -13,19 +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) { - save(); - cairo_set_source_rgba (ctx_, color.r, color.g, color.b, color.a); - cairo_set_operator(ctx_, CAIRO_OPERATOR_SOURCE); - cairo_paint(ctx_); - restore(); + fill(color.r, color.g, color.b, color.a); } void DrawContext::save() { From a38fe407e9016395b43406eadc5e6f63e95fe01e Mon Sep 17 00:00:00 2001 From: 5cript Date: Sun, 21 Jul 2019 03:09:39 +0200 Subject: [PATCH 9/9] Added guards, for pi macro. Since its available on linux --- arrow.cpp | 18 ++++++++++-------- round_rectangle.cpp | 4 +++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/arrow.cpp b/arrow.cpp index ba25ba0..d06d30e 100644 --- a/arrow.cpp +++ b/arrow.cpp @@ -2,10 +2,12 @@ #include -#define M_PI 3.14159265358979323846264338327950288 - -namespace Cairo -{ +#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} @@ -13,8 +15,8 @@ namespace Cairo , arrowLength_{arrowLength} { - } -//--------------------------------------------------------------------------------------------------------------------- + } +//--------------------------------------------------------------------------------------------------------------------- void Arrow::draw(Pen const& line, Pen const& fill) { Line::draw(line); @@ -33,6 +35,6 @@ namespace Cairo cairo_move_to(*ctx_, x2_, y2_); cairo_line_to(*ctx_, x2, y2); stroke(ctx_, fill); - } -//##################################################################################################################### + } +//##################################################################################################################### } diff --git a/round_rectangle.cpp b/round_rectangle.cpp index f7fdb28..6c2962a 100644 --- a/round_rectangle.cpp +++ b/round_rectangle.cpp @@ -2,7 +2,9 @@ #include -#define M_PI 3.14159265358979323846264338327950288 +#ifndef M_PI +# define M_PI 3.14159265358979323846264338327950288 +#endif namespace Cairo {