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

add the 'draw/rectangle' command

parent 4080f055
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "plot/polygons.h"
#include "plot/rectangles.h"
#include "plot/vectors.h"
#include "draw/rectangle.h"
#include "figure/legend.h"

namespace clip {
@@ -34,6 +35,7 @@ const CommandMap COMMANDS = {
  {"height", CommandFn(&context_configure)},
  {"dpi", CommandFn(&context_configure)},
  {"layout/margins", CommandFn(&layout_add_margins)},
  {"draw/rectangle", CommandFn(&draw::rectangle)},
  {"plot/axes", CommandFn(&elements::plot::axis::axis_add_all)},
  {"plot/areas", CommandFn(&elements::plot::areas::areas_draw)},
  {"plot/axis", CommandFn(&elements::plot::axis::axis_draw)},

src/draw/rectangle.cc

0 → 100644
+74 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "clip" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "rectangle.h"
#include "data.h"
#include "sexpr.h"
#include "sexpr_conv.h"
#include "sexpr_util.h"
#include "context.h"
#include "color_reader.h"
#include "style_reader.h"
#include "typographic_map.h"
#include "typographic_reader.h"
#include "layout.h"
#include "marker.h"
#include "scale.h"
#include "graphics/path.h"
#include "graphics/brush.h"
#include "graphics/text.h"
#include "graphics/layout.h"

#include <numeric>

using namespace std::placeholders;
using std::bind;

namespace clip::draw {

ReturnCode rectangle(
    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_with_defaults(expr_next(expr), ctx->defaults, {
    {
      "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;
}

} // namespace clip::draw

src/draw/rectangle.h

0 → 100644
+24 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "clip" project
 *   Copyright (c) 2020 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include "context.h"

namespace clip::draw {

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

} // namespace clip::elements::plot::points
+5 −0
Original line number Diff line number Diff line
@@ -21,6 +21,11 @@ void draw_polygon(
    const Poly2& poly,
    StrokeStyle stroke_style,
    FillStyle fill_style) {
  convert_unit_typographic(
      ctx->dpi,
      ctx->font_size,
      &stroke_style.line_width);

  draw_cmd::Polygon elem;
  elem.poly = poly;
  elem.stroke_style = stroke_style;