Commit 0c933a7a authored by Paul Asmuth's avatar Paul Asmuth
Browse files

split Context into Layer and PlotConfig

parent 351ac846
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ int main(int argc, const char** argv) {
  ReturnCode export_rc;
  switch (output_format) {
    case OutputFormat::SVG:
      export_rc = export_svg(&ctx, &output_buffer);
      export_rc = export_svg(ctx.layer.get(), &output_buffer);
      break;
    //case OutputFormat::PNG:
    //  export_rc = page_export_png(page, drawlist, output_buffer);
+8 −5
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ ReturnCode color_read(
    const Context* ctx,
    const Expr* expr,
    Color* color) {
  const auto layer = layer_get(ctx);

  if (expr_is_value(expr)) {
    const auto value = expr_get_value(expr);

@@ -36,9 +38,9 @@ ReturnCode color_read(

    // color palette index
    if (StringUtil::isDigitString(value)) {
      if (!ctx->color_palette.empty()) {
        *color = ctx->color_palette[
            (std::stol(value) - 1) % ctx->color_palette.size()];
      if (!layer->color_palette.empty()) {
        *color = layer->color_palette[
            (std::stol(value) - 1) % layer->color_palette.size()];
      }

      return OK;
@@ -91,6 +93,7 @@ ReturnCode color_read_string(
    const Context* ctx,
    const std::string& value,
    Color* color) {
  const auto layer = layer_get(ctx);

  // hex code
  if (StringUtil::beginsWith(value, "#")) {
@@ -101,8 +104,8 @@ ReturnCode color_read_string(

  // color palette index
  if (StringUtil::isDigitString(value)) {
    if (!ctx->color_palette.empty()) {
      *color = ctx->color_palette[std::stoul(value) % ctx->color_palette.size()];
    if (!layer->color_palette.empty()) {
      *color = layer->color_palette[std::stoul(value) % layer->color_palette.size()];
    }

    return OK;
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ const CommandMap COMMANDS = {
  //{"height", CommandFn(&context_configure)},
  //{"dpi", CommandFn(&context_configure)},
  //{"draw/rectangle", CommandFn(&draw::rectangle)},
  {"tools/plotgen", CommandFn(&plotgen)},
  {"tools/plotgen", CommandFn(&plot_eval)},
};

} // namespace clip
+3 −81
Original line number Diff line number Diff line
@@ -23,89 +23,11 @@ using namespace std::placeholders;
namespace clip {

Context::Context() :
    width(from_px(1024)),
    height(from_px(512)),
    dpi(96),
    font_defaults(true),
    background_color(Color::fromRGB(1, 1, 1)),
    foreground_color(Color::fromRGB(0, 0, 0)),
    text_color(Color::fromRGB(0, 0, 0)),
    font_size(from_pt(11)),
    color_palette(color_palette_default()) {}
    layer(new Layer()),
    font_defaults(true) {}

ReturnCode context_setup_defaults(Context* ctx) {
  if (ctx->font_defaults) {
    if (auto rc = font_load_defaults(&ctx->font); !rc) {
      return rc;
    }
  }

  for (const auto& f : ctx->font_load) {
    if (auto rc = font_load_best(f, &ctx->font); !rc) {
      return rc;
    }
  }

  return OK;
}

Rectangle context_get_clip(const Context* ctx) {
  if (ctx->layout_stack.empty()) {
    auto margins = ctx->margins;
    for (auto& m : margins) {
      convert_unit_typographic(ctx->dpi, context_get_rem(ctx), &m);
    }

    return layout_margin_box(
        Rectangle(0, 0, ctx->width, ctx->height),
        margins[0],
        margins[1],
        margins[2],
        margins[3]);
  } else {
    return ctx->layout_stack.back();
  }
}

Measure context_get_rem(const Context* ctx) {
  auto rem_default = from_px(16);
  auto rem = ctx->font_size;
  convert_unit_typographic(ctx->dpi, rem_default, &rem);
  return rem;
}

ReturnCode context_set_background(
    Context* ctx,
    const Expr* expr) {
  Rectangle rect = context_get_clip(ctx);
  FillStyle fill_style;
  StrokeStyle stroke_style;
  stroke_style.line_width = from_pt(1);

  /* read arguments */
  auto config_rc = expr_walk_map(expr, {
    {
      "color",
      expr_calln_fn({
        bind(&color_read, ctx, _1, &stroke_style.color),
        bind(&fill_style_read_solid, ctx, _1, &fill_style),
      })
    },
    {"fill", bind(&fill_style_read, ctx, _1, &fill_style)},
    {"stroke-color", bind(&color_read, ctx, _1, &stroke_style.color)},
    {"stroke-width", bind(&measure_read, _1, &stroke_style.line_width)},
    {"stroke-style", bind(&stroke_style_read, ctx, _1, &stroke_style)},
  });

  if (!config_rc) {
    return config_rc;
  }

  Path p;
  path_add_rectangle(&p, rect);
  draw_path(ctx, p, stroke_style, fill_style);

  return OK;
  return layer_create(ctx, &ctx->layer);
}

} // namespace clip
+2 −29
Original line number Diff line number Diff line
@@ -17,46 +17,19 @@
#include "graphics/geometry.h"
#include "color_palette.h"
#include "scale.h"
#include "layer.h"

namespace clip {

struct Context {
  Context();

  Measure width;
  Measure height;
  double dpi;

  std::unique_ptr<Layer> layer;
  bool font_defaults;
  std::vector<std::string> font_load;
  FontInfo font;
  Measure font_size;

  std::string text_default_script;
  std::string text_default_language;

  ColorPalette color_palette;
  Color background_color;
  Color foreground_color;
  Color text_color;

  std::array<Measure, 4> margins;
  std::vector<Rectangle> layout_stack;

  DrawCommandList drawlist;

  ScaleConfig scale_x;
  ScaleConfig scale_y;
};

ReturnCode context_setup_defaults(Context* ctx);

Rectangle context_get_clip(const Context* ctx);

Measure context_get_rem(const Context* ctx);

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

} // namespace clip

Loading