This commit is contained in:
Tim Ebbeke 2021-08-22 01:14:20 +02:00
commit d436eea920
16 changed files with 160 additions and 19 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
build/
build/*

View File

@ -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 "$<$<CONFIG:DEBUG>:${DEBUG_OPTIONS}>")
set(RELEASE_OPTIONS -fexceptions -O3 -Wall -pedantic)
target_compile_options(cairo_wrap PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_OPTIONS}>")
target_compile_options(${cairo-wrap} PUBLIC -fexceptions -std=c++14 -O3 -Wall -pedantic-errors -pedantic)

40
arrow.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "arrow.hpp"
#include <cmath>
#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);
}
//#####################################################################################################################
}

18
arrow.hpp Normal file
View File

@ -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_;
};
}

View File

@ -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 <double> (r) / colorMax,
static_cast <double> (g) / colorMax,
static_cast <double> (b) / colorMax,
static_cast <double> (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()
{

View File

@ -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();

View File

@ -11,7 +11,7 @@ namespace Cairo
void draw(Pen const& line, Pen const& fill = {}) override;
private:
protected:
double x2_;
double y2_;
};

26
path.cpp Normal file
View File

@ -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);
}
}

21
path.hpp Normal file
View File

@ -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);
};
}

View File

@ -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 <double> (pen.color.r) / colorMax,
static_cast <double> (pen.color.g) / colorMax,
static_cast <double> (pen.color.b) / colorMax,
static_cast <double> (pen.color.a) / colorMax
);
}
//---------------------------------------------------------------------------------------------------------------------
void stroke(DrawContext* ctx, Pen const& pen, bool preserve)

View File

@ -2,6 +2,10 @@
#include <cmath>
#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)

View File

@ -5,3 +5,4 @@
#include "line.hpp"
#include "text.hpp"
#include "arc.hpp"
#include "path.hpp"

View File

@ -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());
}

View File

@ -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_;

View File

@ -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)
{

View File

@ -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();