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 Original line Diff line number Diff line
@@ -12,6 +12,7 @@
 * limitations under the License.
 * limitations under the License.
 */
 */
#include "arrows.h"
#include "arrows.h"
#include "graphics/brush.h"
#include <functional>
#include <functional>


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


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


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


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


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


Arrow arrow_create_default();
Arrow arrow_create_default();


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


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


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


namespace fviz {
namespace fviz {
class Layer;


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


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


  LayerRef layer;
  // prepare the page
  ReturnCode layer_rc;
  Page page;
  switch (output_format) {
  page.font = env.font;
    case OutputFormat::PNG:
  page.text_default_script = env.text_default_script;
      layer_rc = layer_bind_png(
  page.text_default_language = env.text_default_language;
          env.screen_width,
  page.width = env.screen_width;
          env.screen_height,
  page.height = env.screen_height;
          env.dpi,
  page.dpi = env.dpi;
          env.font_size,
  page.font_size = env.font_size;
          env.background_color,
  page.background_color = 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;


  LayoutInfo layout;
  LayoutInfo layout;
  layout.content_box = layout_margin_box(
  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[0],
      env.margins[1],
      env.margins[1],
      env.margins[2],
      env.margins[2],
      env.margins[3]);
      env.margins[3]);


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

  // draw all elements
  for (const auto& e : roots) {
  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 rc;
    }
    }
  }
  }


      return OK;
  // export the page
    },
  ReturnCode export_rc;
    [&] { return layer_submit(layer.get()); },
  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(
ReturnCode eval(
Loading