Skip to content

Commit

Permalink
Merge branch 'master' into website
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed May 13, 2020
2 parents 9ce5301 + 11cd2df commit 2ac3529
Show file tree
Hide file tree
Showing 173 changed files with 1,115 additions and 1,109 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
@@ -1,5 +1,5 @@
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")


Expand Down Expand Up @@ -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()
Expand Down
161 changes: 62 additions & 99 deletions src/cli.cc
Expand Up @@ -16,6 +16,7 @@
#include <iostream>
#include <iterator>
#include <string>
#include <getopt.h>

#include "config.h"
#include "context.h"
Expand All @@ -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);

bool flag_help = false;
flag_parser.defineSwitch("help", &flag_help);
flags_add_string(&flags, 'f', "export-format", &flag_format);

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

if (flag_version) {
Expand All @@ -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"
" --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"
"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"
"\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;
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -187,29 +148,31 @@ int main(int argc, const char** argv) {
return EXIT_FAILURE;
}

/* write the output file */
std::string output_buffer;
ReturnCode export_rc;
switch (output_format) {
case OutputFormat::SVG:
export_rc = export_svg(ctx.layer.get(), &output_buffer);
break;
//case OutputFormat::PNG:
// export_rc = page_export_png(page, drawlist, output_buffer);
// break;
}
/* export the context's layers if requested */
if (!flag_export.empty()) {
std::string output_buffer;
ReturnCode export_rc;
switch (output_format) {
case OutputFormat::SVG:
export_rc = export_svg(ctx.layer.get(), &output_buffer);
break;
//case OutputFormat::PNG:
// export_rc = page_export_png(page, drawlist, output_buffer);
// break;
}

if (!export_rc) {
error_print(export_rc, std::cerr);
return EXIT_FAILURE;
}
if (!export_rc) {
error_print(export_rc, std::cerr);
return EXIT_FAILURE;
}

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

return EXIT_SUCCESS;
Expand Down
52 changes: 33 additions & 19 deletions src/plot/areas.cc
Expand Up @@ -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) {
Expand Down Expand Up @@ -149,7 +149,7 @@ ReturnCode plot_areas_horizontal(
return OK;
}

ReturnCode plot_areas_vertical(
ReturnCode areas_draw_vertical(
Context* ctx,
PlotConfig* plot,
PlotAreaConfig config) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/plot/areas.h
Expand Up @@ -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);
Expand Down

0 comments on commit 2ac3529

Please sign in to comment.