Commit 743eaf41 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

clean up core drawing api

parent f8501c8b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@

# chart
add_library(signaltk-graphics STATIC
    path.cc
    colour.cc
    backend_cairo.cc)

#add_executable(test-cplot chart_test.cc)
+24 −39
Original line number Diff line number Diff line
@@ -41,31 +41,6 @@ void CairoBackend::clear(double r, double g, double b, double a) {
  cairo_fill(ctx_);
}

void CairoBackend::drawRect(
    double x,
    double y,
    double width,
    double height,
    const std::string& color,
    const std::string& class_name,
    const std::string& label,
    const std::string& series) {}

void CairoBackend::drawLine(
    double x1,
    double y1,
    double x2,
    double y2,
    const std::string& class_name) {
  cairo_set_source_rgb(ctx_, 0, 0, 0);
  cairo_set_line_width(ctx_, 0.5);

  cairo_move_to(ctx_, x1, y1);
  cairo_line_to(ctx_, x2, y2);

  cairo_stroke(ctx_);
}

void CairoBackend::drawText(
    const std::string& text,
    double x,
@@ -85,23 +60,33 @@ void CairoBackend::drawPoint(
    const std::string& label /* = "" */,
    const std::string& series /* = "" */) {}

void CairoBackend::drawPath(
    const std::vector<std::pair<double, double>>& points,
    const std::string& line_style,
    double line_width,
    bool smooth,
    const std::string& color,
    const std::string& class_name) {
  if (points.size() < 2) {
void CairoBackend::strokePath(
    const PathData* point_data,
    size_t point_count,
    const StrokeStyle& style) {
  if (point_count < 2) {
    return;
  }

  cairo_set_source_rgba(ctx_, 0, 0, 0, 1);
  cairo_set_line_width(ctx_, line_width);

  cairo_move_to(ctx_, points[0].first, points[0].second);
  for (size_t i = 1; i < points.size(); ++i) {
    cairo_line_to(ctx_, points[i].first, points[i].second);
  cairo_set_source_rgba(
      ctx_,
      style.colour.red(),
      style.colour.green(),
      style.colour.blue(),
      style.colour.alpha());

  cairo_set_line_width(ctx_, style.line_width);

  for (size_t i = 0; i < point_count; ++i) {
    const auto& cmd = point_data[i];
    switch (cmd.command) {
      case PathCommand::MOVE_TO:
        cairo_move_to(ctx_, cmd[0], cmd[1]);
        break;
      case PathCommand::LINE_TO:
        cairo_line_to(ctx_, cmd[0], cmd[1]);
        break;
    }
  }

  cairo_stroke(ctx_);
+4 −24
Original line number Diff line number Diff line
@@ -33,23 +33,6 @@ public:

  void clear(double r, double g, double b, double a);

  void drawRect(
      double x,
      double y,
      double width,
      double height,
      const std::string& color,
      const std::string& class_name,
      const std::string& label,
      const std::string& series) override;

  void drawLine(
      double x1,
      double y1,
      double x2,
      double y2,
      const std::string& class_name) override;

  void drawText(
      const std::string& text,
      double x,
@@ -69,13 +52,10 @@ public:
      const std::string& label /* = "" */,
      const std::string& series /* = "" */) override;

   void drawPath(
      const std::vector<std::pair<double, double>>& points,
      const std::string& line_style,
      double line_width,
      bool smooth,
      const std::string& color,
      const std::string& class_name = "") override;
  void strokePath(
      const PathData* point_data,
      size_t point_count,
      const StrokeStyle& stroke_style) override;

  void beginGroup(const std::string& class_name) override;

+61 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * signaltk is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include "colour.h"

namespace signaltk {

Colour Colour::fromRGBA(double red, double green, double blue, double alpha) {
  return Colour({ red, green, blue, alpha });
}

Colour Colour::fromRGB(double red, double green, double blue) {
  return Colour({ red, green, blue, 1.0 });
}

Colour::Colour(
    const std::initializer_list<double>& components) {
  assert(components.size() <= kMaxComponents);

  size_t i = 0;
  for (auto c : components) {
    components_[i++] = c;
  }
}

double Colour::red() const {
  return components_[0];
}

double Colour::green() const {
  return components_[1];
}

double Colour::blue() const {
  return components_[2];
}

double Colour::alpha() const {
  return components_[3];
}

double Colour::component(size_t idx) const {
  return components_[idx];
}

double Colour::operator[](size_t idx) const {
  return components_[idx];
}

double& Colour::operator[](size_t idx) {
  return components_[idx];
}

} // namespace signaltk

core/graphics/colour.h

0 → 100644
+41 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * signaltk is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#pragma once
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <vector>

namespace signaltk {

class Colour {
public:
  static const size_t kMaxComponents = 4;

  static Colour fromRGB(double red, double green, double blue);
  static Colour fromRGBA(double red, double green, double blue, double alpha);

  Colour(const std::initializer_list<double>& components);

  double red() const;
  double green() const;
  double blue() const;
  double alpha() const;

  double component(size_t idx) const;
  double operator[](size_t idx) const;
  double& operator[](size_t idx);

protected:
  double components_[kMaxComponents];
};

} // namespace signaltk
Loading