Commit 6dd6a50f authored by Paul Asmuth's avatar Paul Asmuth
Browse files

implement new 'box layout'

parent 589d9c64
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -14,9 +14,21 @@ points {
axis {
  position: bottom;
  format: datetime("%H:%M:%S");
  layout: linear(3600, align 3600);
}

axis {
  position: left;
  layout: linear(20);
}

axis {
  position: right;
  layout: linear(10);
}

axis {
  position: top;
  format: datetime("%H:%M:%S");
}
+25 −13
Original line number Diff line number Diff line
@@ -145,20 +145,33 @@ ReturnCode document_load(
ReturnCode document_render_to(
    const Document& tree,
    Layer* layer) {
  Rectangle bbox_layer(0, 0, layer->width, layer->height);

  auto bbox_elem = layout_margin_box(
      bbox_layer,
      60,
      60,
      60,
      60);
  //if (!tree.roots.empty()) {
  //  return {ERROR, "document has no root - empty configuration?"};
  //}
  auto bounding_box = Rectangle(0, 0, layer->width, layer->height);
  auto content_box = layout_margin_box(bounding_box, 20, 20, 20, 20);

  std::vector<Rectangle> element_boxes;
  for (const auto& e : tree.roots) {
    if (auto rc = e->draw(bbox_elem, layer); !rc.isSuccess()) {
    LayoutInfo layout;
    layout.bounding_box = bounding_box;
    layout.constraint = {true, true};
    layout.content_box = content_box;

    if (auto rc = e->layout(*layer, &layout); !rc.isSuccess()) {
      return rc;
    }

    content_box = layout.content_box;
    element_boxes.emplace_back(layout.element_box);
  }

  for (size_t i = 0; i < tree.roots.size(); ++i) {
    const auto& element = tree.roots[i];
    LayoutInfo layout;
    layout.bounding_box = bounding_box;
    layout.constraint = {true, true};
    layout.content_box = content_box;
    layout.element_box = element_boxes[i];

    if (auto rc = element->draw(layout, layer); !rc.isSuccess()) {
      return rc;
    }
  }
@@ -178,7 +191,6 @@ ReturnCode document_render(
    return document_render_svg(doc, filename);
  if (format == "png")
    return document_render_png(doc, filename);

  return ReturnCode::errorf("EARG", "invalid output format: $0", format);
}

+13 −3
Original line number Diff line number Diff line
@@ -35,20 +35,21 @@
#include "plist/plist.h"
#include "utils/return_code.h"
#include "environment.h"
#include "layout.h"

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

using plist::PropertyList;

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

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

template <typename T>
using ElementConfigureAsFn = std::function<ReturnCode (
@@ -58,7 +59,16 @@ using ElementConfigureAsFn = std::function<ReturnCode (
    const Environment&,
    T*)>;

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

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

template <typename T>
using ElementLayoutAsFn = std::function<ReturnCode (const T&, const Layer&, LayoutInfo*)>;

struct Element {
  ElementLayoutFn layout;
  ElementDrawFn draw;
};

+2 −2
Original line number Diff line number Diff line
@@ -37,8 +37,8 @@ namespace plotfx {
using ElementConfigureFn = std::function<ReturnCode (const Document&, const PropertyList&, ElementRef*)>;

static std::unordered_map<std::string, ElementBuilder> elems = {
  {"axis", elem_builder<AxisDefinition>(&axis::configure, &axis::draw)},
  {"points", elem_builder<plot::points::PlotPointsConfig>(&plot::points::configure, &plot::points::draw)},
  {"axis", elem_builder<AxisDefinition>(&axis::configure, &axis::layout, &axis::draw)},
  {"points", elem_builder<plot::points::PlotPointsConfig>(&plot::points::configure, &plot::points::layout, &plot::points::draw)},
};

ReturnCode buildElement(
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ using ElementBuilder = std::function<ReturnCode (
template <typename T>
ElementBuilder elem_builder(
    ElementConfigureAsFn<T> config_fn,
    ElementLayoutAsFn<T> layout_fn,
    ElementDrawAsFn<T> draw_fn);

ReturnCode buildElement(
Loading