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

replace the 'Layer' struct with a retained mode page description

parent 992c3a5b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
 * limitations under the License.
 */
#include "arrows.h"
#include "graphics/brush.h"
#include <functional>

using namespace std::placeholders;
@@ -24,7 +25,7 @@ ReturnCode arrow_draw_default(
    const Point& to,
    const Measure& size,
    const Color& color,
    Layer* layer) {
    Page* layer) {
  auto direction = vec2_normalize(vec2_sub(to, from));
  auto ortho = vec2{direction.y, direction.x * -1};

@@ -39,8 +40,8 @@ ReturnCode arrow_draw_default(
  StrokeStyle line_style;
  line_style.color = color;
  line_style.line_width = size;
  strokeLine(layer, from, to, line_style);

  strokeLine(layer, from, to, line_style);
  fillPath(layer, head_path, color);

  return OK;
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
#include <optional>

#include "graphics/color.h"
#include "graphics/layer.h"
#include "graphics/page_description.h"
#include "graphics/geometry.h"
#include "return_code.h"
#include "sexpr.h"
@@ -29,7 +29,7 @@ using Arrow = std::function<
        const Point& to,
        const Measure& size,
        const Color& color,
        Layer* layer)>;
        Page* page)>;

Arrow arrow_create_default();

+3 −3
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
#include <memory>
#include <string>
#include <functional>
#include "graphics/layer.h"
#include "graphics/page_description.h"
#include "sexpr.h"
#include "return_code.h"

@@ -36,11 +36,11 @@ using ElementConfigureFn = std::function<
using ElementDrawFn = std::function<
    ReturnCode (
        const LayoutInfo& layout,
        Layer* layer)>;
        Page* page)>;

using ElementSizeHintFn = std::function<
    ReturnCode (
        const Layer& layer,
        const Page& page,
        const std::optional<double> max_width,
        const std::optional<double> max_height,
        double* min_width,
+1 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include <memory>
#include "return_code.h"
#include "color_palette.h"
#include "graphics/page_description.h"
#include "graphics/measure.h"
#include "graphics/color.h"
#include "graphics/text.h"
@@ -22,7 +23,6 @@
#include "element_factory.h"

namespace fviz {
class Layer;

struct Environment {
  Environment();
+36 −54
Original line number Diff line number Diff line
@@ -12,8 +12,8 @@
 * limitations under the License.
 */
#include "eval.h"
#include "graphics/layer_pixmap.h"
#include "graphics/layer_svg.h"
#include "graphics/page_export_image.h"
#include "graphics/page_export_svg.h"
#include "layout.h"
#include "sexpr_parser.h"

@@ -24,6 +24,7 @@ ReturnCode eval(
    const Expr* expr,
    const OutputFormat& output_format,
    std::string* output_buffer) {
  // prepare the environment
  if (auto rc = environment_configure(&env, expr); !rc) {
    return rc;
  }
@@ -32,69 +33,50 @@ ReturnCode eval(
    return rc;
  }

  LayerRef layer;
  ReturnCode layer_rc;
  switch (output_format) {
    case OutputFormat::PNG:
      layer_rc = layer_bind_png(
          env.screen_width,
          env.screen_height,
          env.dpi,
          env.font_size,
          env.background_color,
          [output_buffer] (auto png) {
            *output_buffer = png;
            return OK;
          },
          &layer);
      break;
    case OutputFormat::SVG:
      layer_rc = layer_bind_svg(
          env.screen_width,
          env.screen_height,
          env.dpi,
          env.font_size,
          env.background_color,
          [output_buffer] (auto svg) {
            *output_buffer = svg;
            return OK;
          },
          &layer);
      break;
  }

  if (!layer_rc) {
    return layer_rc;
  }

  layer->font = env.font;
  layer->text_default_script = env.text_default_script;
  layer->text_default_language = env.text_default_language;
  // prepare the page
  Page page;
  page.font = env.font;
  page.text_default_script = env.text_default_script;
  page.text_default_language = env.text_default_language;
  page.width = env.screen_width;
  page.height = env.screen_height;
  page.dpi = env.dpi;
  page.font_size = env.font_size;
  page.background_color = env.background_color;

  LayoutInfo layout;
  layout.content_box = layout_margin_box(
      Rectangle(0, 0, layer->width, layer->height),
      Rectangle(0, 0, page.width, page.height),
      env.margins[0],
      env.margins[1],
      env.margins[2],
      env.margins[3]);

  // build all root elements
  std::vector<ElementRef> roots;
  auto rc = try_chain({
    [&] { return element_build_all(env, expr, &roots); },
    [&] () -> ReturnCode {
  if (auto rc = element_build_all(env, expr, &roots); !rc) {
    return rc;
  }

  // draw all elements
  for (const auto& e : roots) {
        if (auto rc = e->draw(layout, layer.get()); !rc) {
    if (auto rc = e->draw(layout, &page); !rc) {
      return rc;
    }
  }

      return OK;
    },
    [&] { return layer_submit(layer.get()); },
  });
  // export the page
  ReturnCode export_rc;
  switch (output_format) {
    case OutputFormat::SVG:
      export_rc = page_export_svg(page, output_buffer);
      break;
    case OutputFormat::PNG:
      export_rc = page_export_png(page, output_buffer);
      break;
  }

  return rc;
  return export_rc;
}

ReturnCode eval(
Loading