Commit 346d5265 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved element::build interface

parent 1543b090
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" "elements/text.cc" "core/element_factory.cc" "core/config_helpers.cc")
file(GLOB source_files "core/graphics/*.cc" "core/utils/**.cc" "core/sexpr*.cc" "core/plotfx.cc" "core/scale.cc" "core/environment.cc" "elements/text.cc" "core/element_factory.cc" "core/config_helpers.cc" "elements/chart/axis.cc" "core/format.cc" "core/layout.cc" "core/data_model.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")
+4 −2
Original line number Diff line number Diff line
@@ -33,23 +33,25 @@
#include <string>
#include <functional>
#include "environment.h"
#include "graphics/layer.h"
#include "sexpr.h"
#include "utils/return_code.h"
#include "graphics/layer.h"

namespace plotfx {
struct LayoutInfo;

struct Element;
using ElementRef = std::shared_ptr<Element>;

using ElementConfigureFn = std::function<
    ReturnCode (
        const Environment& env,
        const Expr* expr,
        ElementRef* elem)>;

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

struct Element {
+4 −2
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
namespace plotfx {

ReturnCode element_build(
    const Environment& env,
    const ElementMap& factory,
    const Expr* expr,
    ElementRef* elem) {
@@ -47,10 +48,11 @@ ReturnCode element_build(
    return ReturnCode::errorf("EARG", "no such element: $0", elem_name);
  }

  return elem_iter->second(expr_next(expr), elem);
  return elem_iter->second(env, expr, elem);
}

ReturnCode element_build_all(
    const Environment& env,
    const ElementMap& factory,
    const Expr* expr,
    std::vector<ElementRef>* elems) {
@@ -60,7 +62,7 @@ ReturnCode element_build_all(
    }

    ElementRef elem;
    if (auto rc = element_build(factory, expr_get_list(expr), &elem); !rc) {
    if (auto rc = element_build(env, factory, expr_get_list(expr), &elem); !rc) {
      return rc;
    }

+2 −0
Original line number Diff line number Diff line
@@ -40,11 +40,13 @@ struct ElementMap {
};

ReturnCode element_build(
    const Environment& env,
    const ElementMap& factory,
    const Expr* expr,
    ElementRef* elem);

ReturnCode element_build_all(
    const Environment& env,
    const ElementMap& factory,
    const Expr* expr,
    std::vector<ElementRef>* elems);
+8 −7
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
 */
#include "plotfx.h"
#include "element_factory.h"
#include "layout.h"
#include "sexpr_parser.h"
#include "graphics/layer.h"
#include "graphics/layer_svg.h"
@@ -52,7 +53,7 @@ struct plotfx_s {

plotfx_t* plotfx_init() {
  auto ctx = std::make_unique<plotfx_t>();
  element_bind(&ctx->elements, "text", bind(elements::text::configure, _1, _2));
  element_bind(&ctx->elements, "text", bind(elements::text::build, _1, _2, _3));
  return ctx.release();
}

@@ -109,20 +110,20 @@ int plotfx_configure_file(
int plotfx_render_to(plotfx_t* ctx, void* backend) {
  auto layer = static_cast<Layer*>(backend);

  //LayoutInfo layout;
  //layout.bounding_box = Rectangle(0, 0, layer->width, layer->height);
  //layout.content_box = layout.bounding_box;
  //layout.inner_box = layout.bounding_box;
  LayoutInfo layout;
  layout.bounding_box = Rectangle(0, 0, layer->width, layer->height);
  layout.content_box = layout.bounding_box;
  layout.inner_box = layout.bounding_box;

  //plot::PlotConfig root;
  //root.margins = {from_px(20), from_px(20), from_px(20), from_px(20)};

  std::vector<ElementRef> roots;
  auto rc = try_chain({
    [&] { return element_build_all(ctx->elements, ctx->expr.get(), &roots); },
    [&] { return element_build_all(ctx->env, ctx->elements, ctx->expr.get(), &roots); },
    [&] () -> ReturnCode {
      for (const auto& e : roots) {
        if (auto rc = e->draw(ctx->env, layer); !rc) {
        if (auto rc = e->draw(layout, layer); !rc) {
          return rc;
        }
      }
Loading