Skip to content

Commit

Permalink
add the 'draw/rectangle' command
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed Feb 1, 2020
1 parent 4080f05 commit 0a78407
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/commands.h
Expand Up @@ -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 {
Expand All @@ -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)},
Expand Down
74 changes: 74 additions & 0 deletions src/draw/rectangle.cc
@@ -0,0 +1,74 @@
/**
* 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

24 changes: 24 additions & 0 deletions src/draw/rectangle.h
@@ -0,0 +1,24 @@
/**
* 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 changes: 5 additions & 0 deletions src/graphics/draw.cc
Expand Up @@ -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;
Expand Down

0 comments on commit 0a78407

Please sign in to comment.