Commit fc82bd13 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

split up cairo backend into layer, brush and text modules

- turn drawing methods into free methods and move out of render target
- remove backend vtable abstraction
- rename remainder of the rendertarget class to "Layer"
parent 9bfbb4ab
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -9,8 +9,10 @@
# chart
add_library(signaltk-graphics STATIC
    path.cc
    brush.cc
    colour.cc
    backend_cairo.cc)
    layer.cc
    text.cc)

#add_executable(test-cplot chart_test.cc)
#target_link_libraries(test-cplot cplot stx-base)
+63 −0
Original line number Diff line number Diff line
@@ -2,79 +2,62 @@
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * libstx is free software: you can redistribute it and/or modify it under
 * 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 <iostream>
#include "backend_cairo.h"
#include "brush.h"

namespace signaltk {

CairoBackend::CairoBackend() :
  width_(1000),
  height_(600) {
  surface_ = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width_, height_);
  ctx_ = cairo_create(surface_);
}

CairoBackend::~CairoBackend() {
  cairo_destroy(ctx_);
  cairo_surface_destroy(surface_);
}

void CairoBackend::writePNG(const char* path) {
  cairo_surface_write_to_png(surface_, path);
}

void CairoBackend::clear(double r, double g, double b, double a) {
  cairo_set_source_rgba(ctx_, r, g, b, a);
  cairo_rectangle(ctx_, 0, 0, width_, height_);
  cairo_fill(ctx_);
}

void CairoBackend::drawText(
    const std::string& text,
    double x,
    double y,
    const std::string& halign,
    const std::string& valign,
    const std::string& class_name,
    double rotate) {
  // here be dragons
}

void CairoBackend::strokePath(
void strokePath(
    Layer* layer,
    const PathData* point_data,
    size_t point_count,
    const StrokeStyle& style) {
  auto ctx = layer->ctx;

  if (point_count < 2) {
    return;
  }

  cairo_set_source_rgba(
      ctx_,
      ctx,
      style.colour.red(),
      style.colour.green(),
      style.colour.blue(),
      style.colour.alpha());

  cairo_set_line_width(ctx_, style.line_width);
  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]);
        cairo_move_to(ctx, cmd[0], cmd[1]);
        break;
      case PathCommand::LINE_TO:
        cairo_line_to(ctx_, cmd[0], cmd[1]);
        cairo_line_to(ctx, cmd[0], cmd[1]);
        break;
    }
  }

  cairo_stroke(ctx_);
  cairo_stroke(ctx);
}

void strokeLine(
    Layer* layer,
    double x1,
    double y1,
    double x2,
    double y2,
    const StrokeStyle& style) {
  Path p;
  p.moveTo(x1, y1);
  p.lineTo(x2, y2);
  strokePath(layer, p.data(), p.size(), style);
}

} // namespace signaltk
+16 −0
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@
#include <stdlib.h>
#include <stdint.h>
#include "colour.h"
#include "layer.h"
#include "path.h"

namespace signaltk {

@@ -31,5 +33,19 @@ struct StrokeStyle {
  Colour colour;
};

void strokePath(
    Layer* layer,
    const PathData* path_data,
    size_t path_data_count,
    const StrokeStyle& style);

void strokeLine(
    Layer* layer,
    double x1,
    double y1,
    double x2,
    double y2,
    const StrokeStyle& style);

} // namespace signaltk
+40 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * libstx 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 "layer.h"

namespace signaltk {

Layer::Layer(
  uint32_t w,
  uint32_t h) :
  width(w),
  height(h) {
  surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  ctx = cairo_create(surface);
}

Layer::~Layer() {
  cairo_destroy(ctx);
  cairo_surface_destroy(surface);
}

void Layer::clear(double r, double g, double b, double a) {
  cairo_set_source_rgba(ctx, r, g, b, a);
  cairo_rectangle(ctx, 0, 0, width, height);
  cairo_fill(ctx);
}

void Layer::writePNG(const char* path) {
  cairo_surface_write_to_png(surface, path);
}

} // namespace signaltk
+14 −35
Original line number Diff line number Diff line
@@ -8,51 +8,30 @@
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#ifndef _libstx_RENDERTARGET_H
#define _libstx_RENDERTARGET_H
#pragma once
#include <stdlib.h>
#include <vector>
#include <string>
#include <cairo.h>
#include "colour.h"
#include "path.h"
#include "brush.h"

namespace signaltk {
namespace chart {

class Layer {
public:
  virtual ~Layer() {}
struct Layer {
  Layer(uint32_t width, uint32_t height);
  ~Layer();
  Layer(const Layer&) = delete;
  Layer& operator=(const Layer&) = delete;

  virtual void drawText(
      const std::string& text,
      double x,
      double y,
      const std::string& halign,
      const std::string& valign,
      const std::string& class_name,
      double rotate = 0.0f) = 0;
  void writePNG(const char* path);

  virtual void strokePath(
      const PathData* point_data,
      size_t point_count,
      const StrokeStyle& style) = 0;

  void strokeLine(
      double x1,
      double y1,
      double x2,
      double y2,
      const StrokeStyle& style) {
    Path p;
    p.moveTo(x1, y1);
    p.lineTo(x2, y2);
    strokePath(p.data(), p.size(), style);
  }
  void clear(double r, double g, double b, double a);

  const uint32_t width;
  const uint32_t height;
  cairo_surface_t* surface;
  cairo_t* ctx;
};

} // namespace signaltk
}
}
#endif
Loading