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

new element factory, add new basic 'text' element

parent ec42955c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ include_directories(${CAIRO_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_IN

# Build: PlotFX Library
# -----------------------------------------------------------------------------
file(GLOB source_files "core/graphics/*.cc" "core/utils/**.cc" "core/sexpr*.cc" "core/plotfx.cc" "core/environment.cc")
file(GLOB source_files "core/graphics/*.cc" "core/utils/**.cc" "core/sexpr*.cc" "core/plotfx.cc" "core/environment.cc" "elements/text.cc" "core/element_factory.cc")
list(REMOVE_ITEM source_files "core/plotfx_cli.cc")
add_library(plotfx STATIC ${source_files})
set_target_properties(plotfx PROPERTIES PUBLIC_HEADER "source/plotfx.h;source/plotfx_sdl.h")
+12 −52
Original line number Diff line number Diff line
@@ -32,70 +32,30 @@
#include <memory>
#include <string>
#include <functional>
#include "environment.h"
#include "sexpr.h"
#include "utils/return_code.h"
#include "core/format.h"
#include "graphics/geometry.h"
#include "graphics/layer.h"

namespace plotfx {
struct Layer;
struct LayoutInfo;
struct LayoutSettings;
struct Document;
struct DataContext;
struct Environment;

using plist::PropertyList;

template <typename T>
using ElementConfigureAsFn = std::function<ReturnCode (
    const plist::PropertyList&,
    const Environment&,
    T*)>;

using ElementDrawFn = std::function<ReturnCode (const LayoutInfo&, Layer*)>;

template <typename T>
using ElementDrawAsFn = std::function<ReturnCode (
    const T&,
    const LayoutInfo&,
    Layer*)>;
struct Element;
using ElementRef = std::shared_ptr<Element>;

using ElementReflowFn = std::function<ReturnCode (
    const Layer&,
    const std::optional<double> max_width,
    const std::optional<double> max_height,
    double* min_width,
    double* min_height)>;
using ElementConfigureFn = std::function<
    ReturnCode (
        const Expr* expr,
        ElementRef* elem)>;

template <typename T>
using ElementReflowAsFn = std::function<ReturnCode (
    const T&,
    const Layer&,
    const std::optional<double> max_width,
    const std::optional<double> max_height,
    double* min_width,
    double* min_height)>;
using ElementDrawFn = std::function<
    ReturnCode (
        const Environment& env,
        Layer* layer)>;

struct Element {
  virtual ~Element() = default;
  virtual const LayoutSettings& layout_settings() const = 0;
  int32_t z_index() const;
  ElementReflowFn reflow;
  ElementDrawFn draw;
};

template <typename T>
struct ElementInstance : public Element {
  T config;

  const LayoutSettings& layout_settings() const override {
    return config.layout;
  }

};

using ElementRef = std::shared_ptr<Element>;

} // namespace plotfx
+19 −77
Original line number Diff line number Diff line
@@ -28,91 +28,33 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "element_factory.h"
#include "elements/areas.h"
#include "elements/axis.h"
#include "elements/bars.h"
#include "elements/box.h"
#include "elements/gridlines.h"
#include "elements/legend.h"
#include "elements/lines.h"
#include "elements/plot.h"
#include "elements/points.h"

#include <unordered_map>

namespace plotfx {

static std::unordered_map<std::string, ElementBuilder> elems = {
  {
    "areas",
    elem_builder<plot::area::PlotAreaConfig>(
        &plot::area::configure,
        &plot::area::draw)
  },
  {
    "axis",
    elem_builder<AxisDefinition>(
        &axis::configure,
        &axis::reflow,
        &axis::draw)
  },
  {
    "bars",
    elem_builder<plot::bars::PlotBarsConfig>(
        &plot::bars::configure,
        &plot::bars::draw)
  },
  {
    "box",
    elem_builder<box::BoxConfig>(
        &box::configure,
        &box::draw)
  },
  {
    "gridlines",
    elem_builder<gridlines::GridlineDefinition>(
        &gridlines::configure,
        &gridlines::draw)
  },
  {
    "legend",
    elem_builder<LegendConfig>(
        &legend::configure,
        &legend::reflow,
        &legend::draw)
  },
  {
    "lines",
    elem_builder<plot::lines::PlotLinesConfig>(
        &plot::lines::configure,
        &plot::lines::draw)
  },
  {
    "plot",
    elem_builder<plot::PlotConfig>(
        &plot::configure,
        &plot::draw)
  },
  {
    "points",
    elem_builder<plot::points::PlotPointsConfig>(
        &plot::points::configure,
        &plot::points::draw)
  },
};

ReturnCode buildElement(
    const std::string& name,
    const plist::PropertyList& plist,
    const Environment& env,
ReturnCode element_build(
    const ElementMap& factory,
    const Expr* expr,
    ElementRef* elem) {
  const auto& elem_entry = elems.find(name);
  if (elem_entry == elems.end()) {
    return ReturnCode::errorf("NOTFOUND", "no such element: $0", name);
  if (!expr || !expr_is_value(expr)) {
    return ReturnCode::error("EARG", "expected an element name");
  }

  return elem_entry->second(plist, env, elem);
  auto elem_name = expr_get_value(expr);
  auto elem_iter = factory.elements.find(elem_name);
  if (elem_iter == factory.elements.end()) {
    return ReturnCode::errorf("EARG", "no such element: $0", elem_name);
  }

} // namespace plotfx
  return elem_iter->second(expr_next(expr), elem);
}

void element_bind(
    ElementMap* factory,
    const std::string& name,
    ElementConfigureFn configure_fn) {
  factory->elements[name] = configure_fn;
}

} // namespace plotfx
+12 −22
Original line number Diff line number Diff line
@@ -28,35 +28,25 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
#include <unordered_map>

#include "utils/return_code.h"
#include "element.h"

namespace plotfx {
struct Document;
struct Environment;

using ElementBuilder = std::function<ReturnCode (
    const plist::PropertyList&,
    const Environment&,
    ElementRef* elem)>;

template <typename T>
ElementBuilder elem_builder(
    ElementConfigureAsFn<T> config_fn,
    ElementDrawAsFn<T> draw_fn);
struct ElementMap {
  std::unordered_map<std::string, ElementConfigureFn> elements;
};

template <typename T>
ElementBuilder elem_builder(
    ElementConfigureAsFn<T> config_fn,
    ElementReflowAsFn<T> layout_fn,
    ElementDrawAsFn<T> draw_fn);
ReturnCode element_build(
    const ElementMap& factory,
    const Expr* expr,
    ElementRef* elem);

ReturnCode buildElement(
void element_bind(
    ElementMap* factory,
    const std::string& name,
    const plist::PropertyList& plist,
    const Environment& env,
    ElementRef* elem);
    ElementConfigureFn configure_fn);

} // namespace plotfx

#include "element_factory_impl.h"
+2 −0
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ struct Environment {
  Color border_color;
};

ReturnCode environment_setup_defaults(Environment* env);

} // namespace plotfx

Loading