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

implement the 'default' parameter mechanism

parent e3b7d6dd
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -158,7 +158,6 @@ int main(int argc, const char** argv) {
  Context ctx;
  ctx.font_defaults = flag_font_defaults;
  ctx.font_load = flag_font_load;
  ctx.layout_stack.push_back(Rectangle(0, 0, ctx.width, ctx.height));

  if (auto rc = context_setup_defaults(&ctx); !rc) {
    error_print(rc, std::cerr);
+4 −0
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@
namespace clip {

const CommandMap COMMANDS = {
  {"default", CommandFn(&context_set_default)},
  {"set-width", CommandFn(&context_configure)},
  {"set-height", CommandFn(&context_configure)},
  {"set-dpi", CommandFn(&context_configure)},
  {"layout/add-margins", CommandFn(&layout_add_margins)},
  {"plot/add-axes", CommandFn(&elements::plot::axis::axis_add_all)},
  {"plot/draw-axis", CommandFn(&elements::plot::axis::axis_draw)},
+41 −3
Original line number Diff line number Diff line
@@ -12,12 +12,15 @@
 * limitations under the License.
 */
#include "context.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include "typographic_reader.h"

namespace clip {

Context::Context() :
    width(1024),
    height(512),
    width(from_px(1024)),
    height(from_px(512)),
    dpi(96),
    font_defaults(true),
    background_color(Color::fromRGB(1, 1, 1)),
@@ -42,9 +45,44 @@ ReturnCode context_setup_defaults(Context* ctx) {
  return OK;
}

ReturnCode context_configure(Context* ctx, const Expr* expr) {
  auto args = expr_collect(expr);
  if (args.size() < 2) {
    return error(ERROR, "Expected at least two arguments");
  }

  if (expr_is_value(args[0], "set-width")) {
    return measure_read(args[1], &ctx->width);
  }

  if (expr_is_value(args[0], "set-height")) {
    return measure_read(args[1], &ctx->height);
  }

  if (expr_is_value(args[0], "set-dpi")) {
    return expr_to_float64(args[1], &ctx->dpi);
  }

  return errorf(ERROR, "Unknown command: {}", expr_get_value(args[0]));
}

ReturnCode context_set_default(Context* ctx, const Expr* expr) {
  auto args = expr_collect(expr);
  if (args.size() != 3) {
    return error(ERROR, "Expected exactly two arguments");
  }

  if (!expr_is_value_literal(args[1])) {
    return error(ERROR, "The first argument to 'default' must be a string literal.");
  }

  ctx->defaults[expr_get_value(args[1])] = expr_clone(args[2]);
  return OK;
}

Rectangle context_get_clip(const Context* ctx) {
  if (ctx->layout_stack.empty()) {
    return Rectangle(0, 0, 0, 0);
    return Rectangle(0, 0, ctx->width, ctx->height);
  } else {
    return ctx->layout_stack.back();
  }
+7 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ namespace clip {
struct Context {
  Context();

  double width;
  double height;
  Measure width;
  Measure height;
  double dpi;

  bool font_defaults;
@@ -42,10 +42,15 @@ struct Context {
  DrawCommandList drawlist;
  std::vector<Rectangle> layout_stack;

  std::unordered_map<std::string, ExprStorage> defaults;
};

ReturnCode context_setup_defaults(Context* ctx);

ReturnCode context_configure(Context* ctx, const Expr* expr);

ReturnCode context_set_default(Context* ctx, const Expr* expr);

Rectangle context_get_clip(const Context* ctx);

} // namespace clip
+1 −1
Original line number Diff line number Diff line
@@ -485,7 +485,7 @@ ReturnCode legend_draw(
  }

  /* parse exprerties */
  auto config_rc = expr_walk_map(expr_next(expr), {
  auto config_rc = expr_walk_map_with_defaults(expr_next(expr), ctx->defaults, {
    {
      "position",
      bind(
Loading