Commit 2ac3529a authored by Paul Asmuth's avatar Paul Asmuth
Browse files

Merge branch 'master' into website

parents 9ce5301f 11cd2df2
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.8)
project(clip VERSION "0.6.0")
project(clip VERSION "0.7.0")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/extra/cmake")


@@ -130,7 +130,7 @@ foreach(example_path ${example_files})
  add_custom_target(
      example_${example_name}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      COMMAND ${CMAKE_CURRENT_BINARY_DIR}/clip --in ${example_srcdir}/${example_name}.clp --out ${example_srcdir}/${example_name}.svg)
      COMMAND ${CMAKE_CURRENT_BINARY_DIR}/clip ${example_srcdir}/${example_name}.clp -e ${example_srcdir}/${example_name}.svg)
  add_dependencies(examples example_${example_name})
  add_dependencies(example_${example_name} clip-cli)
endforeach()
+62 −99
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <iostream>
#include <iterator>
#include <string>
#include <getopt.h>

#include "config.h"
#include "context.h"
@@ -32,45 +33,31 @@ void printError(const ReturnCode& rc) {
  std::cerr << fmt::format("ERROR: {}", rc.message) << std::endl;
}

int main(int argc, const char** argv) {
  FlagParser flag_parser;
int main(int argc, char** argv) {
  FlagList flags;

  std::string flag_in;
  flag_parser.defineString("in", false, &flag_in);

  std::string flag_out;
  flag_parser.defineString("out", false, &flag_out);

  bool flag_stdin = false;
  flag_parser.defineSwitch("stdin", &flag_stdin);

  bool flag_stdout = false;
  flag_parser.defineSwitch("stdout", &flag_stdout);
  std::string flag_export;
  flags_add_string(&flags, 'e', "export", &flag_export);

  std::string flag_format;
  flag_parser.defineString("format", false, &flag_format);
  flags_add_string(&flags, 'f', "export-format", &flag_format);

  bool flag_help = false;
  flag_parser.defineSwitch("help", &flag_help);

  bool flag_version = false;
  flag_parser.defineSwitch("version", &flag_version);
  std::vector<std::string> flag_font_load;
  flags_add_stringv(&flags, 0, "font-load", &flag_font_load);

  bool flag_debug = true;
  flag_parser.defineSwitch("debug", &flag_debug);
  flags_add_switch(&flags, 'd', "debug", &flag_debug);

  bool flag_font_defaults = true;
  flag_parser.defineBool("font-defaults", &flag_font_defaults);
  bool flag_help = false;
  flags_add_switch(&flags, 'h', "help", &flag_help);

  std::vector<std::string> flag_font_load;
  flag_parser.defineStringV("font-load", &flag_font_load);
  bool flag_version = false;
  flags_add_switch(&flags, 'v', "version", &flag_version);

  {
    auto rc = flag_parser.parseArgv(argc - 1, argv + 1);
    if (!rc) {
  std::vector<std::string> args;
  if (auto rc = flags_parse(flags, argc, argv, &args); !rc) {
    std::cerr << "ERROR: " << rc.message << std::endl;
      return -1;
    }
    return EXIT_FAILURE;
  }

  if (flag_version) {
@@ -85,61 +72,35 @@ int main(int argc, const char** argv) {
    return 0;
  }

  if (flag_help) {
  if (flag_help || args.size() != 1) {
    std::cerr <<
        "Usage: $ clip [OPTIONS]\n"
        "  --in <path>               Path to the input file\n"
        "  --out <path>              Path to the output file\n"
        "  --stdin                   Read the input file from stdin\n"
        "  --stdout                  Write the output file from sdout\n"
        "  --format <format>         Output format. If no format is given, it is inferred from the\n"
        "                            filename. Valid values: 'png', 'svg'\n"
        "  --font-defaults <bool>    Enable or disable default font loading. Default is enabled.\n"
        "                            Valid values: 'on' and 'off'\n"
        "Usage: clip [OPTIONS] <file>\n"
        "\n"
        "Export:\n"
        "  -e, --export <path>          Export the image\n"
        "  -f, --export-format <fmt>    Export format. If no format is given, it is\n"
        "                               inferred from the export filename.\n"
        "\n"
        "Fonts:\n"
        "  --font-load <path>           Add a font file to the default font list\n"
        "  --debug                   Run in debug mode\n"
        "  --help                    Display this help text and exit\n"
        "  --version                 Display the version of this binary and exit\n"
        "\n"
        "Other:\n"
        "  -d, --debug                  Run in debug mode\n"
        "  -h, --help                   Display this help text and exit\n"
        "  -v, --version                Display the version of this binary and exit\n"
        "\n"
        "Examples:\n"
        "  $ clip --in my_chart.clp --out my_chart.svg\n";
        "  $ clip -e my_chart.svg my_chart.clp\n";

    return 0;
  }

  /* check if the input flags are valid */
  if (flag_in.empty() && !flag_stdin) {
    std::cerr << "Need an input file (--in)\n";
    return 1;
  }

  if (!flag_in.empty() && flag_stdin) {
    std::cerr
        << "Can't read from an input file (--in) and stdin (--stdin) at the "
        << "same time\n";

    return 1;
  }

  if (flag_out.empty() && !flag_stdout) {
    std::cerr << "Need an output file (--out)\n";
    return 1;
  }

  if (!flag_out.empty() && flag_stdout) {
    std::cerr
        << "Can't write to an output file (--out) and stdout (--stdout) at the "
        << "same time\n";

    return 1;
  }

  /* figure out which output format the user wants */
  auto output_format = clip::OutputFormat::SVG;
  if (flag_format.empty()) {
    if (StringUtil::endsWith(flag_out, ".svg"))
    if (StringUtil::endsWith(flag_export, ".svg"))
      output_format = OutputFormat::SVG;
    if (StringUtil::endsWith(flag_out, ".png"))
    if (StringUtil::endsWith(flag_export, ".png"))
      output_format = OutputFormat::PNG;
  } else if (flag_format == "svg") {
    output_format = OutputFormat::SVG;
@@ -156,7 +117,6 @@ int main(int argc, const char** argv) {

  /* set up the context */
  Context ctx;
  ctx.font_defaults = flag_font_defaults;
  ctx.font_load = flag_font_load;

  if (auto rc = context_setup_defaults(&ctx); !rc) {
@@ -166,15 +126,16 @@ int main(int argc, const char** argv) {

  /* read the input file */
  std::string input;
  if (flag_stdin) {
  const auto& input_path = args[0];
  if (input_path == "-") {
    std::istreambuf_iterator<char> begin(std::cin), end;
    input = std::string(begin, end);
  } else {
    if (auto rc = read_file(flag_in, &input); !rc) {
    if (auto rc = read_file(input_path, &input); !rc) {
      fmt::print(
          stderr,
          "ERROR: unable to read input file ({}): {}\n",
          flag_in,
          input_path,
          rc.message);

      return EXIT_FAILURE;
@@ -187,7 +148,8 @@ int main(int argc, const char** argv) {
    return EXIT_FAILURE;
  }

  /* write the output file */
  /* export the context's layers if requested */
  if (!flag_export.empty()) {
    std::string output_buffer;
    ReturnCode export_rc;
    switch (output_format) {
@@ -204,13 +166,14 @@ int main(int argc, const char** argv) {
      return EXIT_FAILURE;
    }

  if (flag_stdout) {
    if (flag_export == "-") {
      std::cout << output_buffer;
    } else {
      FileUtil::write(
        flag_out,
          flag_export,
          Buffer(output_buffer.data(), output_buffer.size()));
    }
  }

  return EXIT_SUCCESS;
}
+33 −19
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ struct PlotAreaConfig {
PlotAreaConfig::PlotAreaConfig() :
    direction(Direction::VERTICAL) {}

ReturnCode plot_areas_horizontal(
ReturnCode areas_draw_horizontal(
    Context* ctx,
    PlotConfig* plot,
    PlotAreaConfig config) {
@@ -149,7 +149,7 @@ ReturnCode plot_areas_horizontal(
  return OK;
}

ReturnCode plot_areas_vertical(
ReturnCode areas_draw_vertical(
    Context* ctx,
    PlotConfig* plot,
    PlotAreaConfig config) {
@@ -247,26 +247,12 @@ ReturnCode plot_areas_vertical(
  return OK;
}

ReturnCode plot_areas(
    Context* ctx,
    PlotConfig* plot,
    std::shared_ptr<PlotAreaConfig> config) {
  switch (config->direction) {
    case Direction::HORIZONTAL:
      return plot_areas_horizontal(ctx, plot, *config);
    case Direction::VERTICAL:
      return plot_areas_vertical(ctx, plot, *config);
    default:
      return ERROR;
  }
}

ReturnCode plot_areas(
ReturnCode areas_configure(
    Context* ctx,
    PlotConfig* plot,
    PlotAreaConfig* c,
    const Expr* expr) {
  /* set defaults from environment */
  auto c = std::make_shared<PlotAreaConfig>();
  c->scale_x = plot->scale_x;
  c->scale_y = plot->scale_y;
  c->stroke_high_style.color = layer_get(ctx)->foreground_color;
@@ -407,7 +393,35 @@ ReturnCode plot_areas(
        "the length of the 'data-y' and 'data-y-low' properties must be equal");
  }

  return plot_areas(ctx, plot, c);
  return OK;
}

ReturnCode areas_draw(
    Context* ctx,
    PlotConfig* plot,
    const Expr* expr) {
  PlotAreaConfig conf;

  if (auto rc = areas_configure(ctx, plot, &conf, expr); !rc) {
    return rc;
  }

  switch (conf.direction) {
    case Direction::HORIZONTAL:
      return areas_draw_horizontal(ctx, plot, conf);
    case Direction::VERTICAL:
      return areas_draw_vertical(ctx, plot, conf);
    default:
      return ERROR;
  }
}

ReturnCode areas_autorange(
    Context* ctx,
    PlotConfig* plot,
    const Expr* expr) {
  PlotAreaConfig conf;
  return areas_configure(ctx, plot, &conf, expr);
}

} // namespace clip::plotgen
+6 −1
Original line number Diff line number Diff line
@@ -16,7 +16,12 @@

namespace clip::plotgen {

ReturnCode plot_areas(
ReturnCode areas_draw(
    Context* ctx,
    PlotConfig* plot,
    const Expr* expr);

ReturnCode areas_autorange(
    Context* ctx,
    PlotConfig* plot,
    const Expr* expr);
+33 −19
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ struct PlotBarsConfig {
PlotBarsConfig::PlotBarsConfig() :
    direction(Direction::VERTICAL) {}

ReturnCode plot_bars_horizontal(
ReturnCode bars_draw_horizontal(
    Context* ctx,
    PlotConfig* plot,
    PlotBarsConfig config) {
@@ -174,7 +174,7 @@ ReturnCode plot_bars_horizontal(
  return OK;
}

ReturnCode plot_bars_vertical(
ReturnCode bars_draw_vertical(
    Context* ctx,
    PlotConfig* plot,
    PlotBarsConfig config) {
@@ -289,26 +289,12 @@ ReturnCode plot_bars_vertical(
  return OK;
}

ReturnCode plot_bars(
    Context* ctx,
    PlotConfig* plot,
    std::shared_ptr<PlotBarsConfig> config) {
  switch (config->direction) {
    case Direction::HORIZONTAL:
      return plot_bars_horizontal(ctx, plot, *config);
    case Direction::VERTICAL:
      return plot_bars_vertical(ctx, plot, *config);
    default:
      return ERROR;
  }
}

ReturnCode plot_bars(
ReturnCode bars_configure(
    Context* ctx,
    PlotConfig* plot,
    PlotBarsConfig* c,
    const Expr* expr) {
  /* set defaults from environment */
  auto c = std::make_shared<PlotBarsConfig>();
  c->scale_x = plot->scale_x;
  c->scale_y = plot->scale_y;
  c->stroke_style.color = layer_get(ctx)->foreground_color;
@@ -434,7 +420,35 @@ ReturnCode plot_bars(
        "the length of the 'data-y' and 'data-y-low' properties must be equal");
  }

  return plot_bars(ctx, plot, c);
  return OK;
}

ReturnCode bars_draw(
    Context* ctx,
    PlotConfig* plot,
    const Expr* expr) {
  PlotBarsConfig conf;

  if (auto rc = bars_configure(ctx, plot, &conf, expr); !rc) {
    return rc;
  }

  switch (conf.direction) {
    case Direction::HORIZONTAL:
      return bars_draw_horizontal(ctx, plot, conf);
    case Direction::VERTICAL:
      return bars_draw_vertical(ctx, plot, conf);
    default:
      return ERROR;
  }
}

ReturnCode bars_autorange(
    Context* ctx,
    PlotConfig* plot,
    const Expr* expr) {
  PlotBarsConfig conf;
  return bars_configure(ctx, plot, &conf, expr);
}

} // namespace clip::plotgen
Loading