From 0a78407bbaba0609daf0f9dca3c8f27d4d86ad20 Mon Sep 17 00:00:00 2001 From: Paul Asmuth Date: Sun, 2 Feb 2020 00:14:40 +0100 Subject: [PATCH] add the 'draw/rectangle' command --- src/commands.h | 2 ++ src/draw/rectangle.cc | 74 +++++++++++++++++++++++++++++++++++++++++++ src/draw/rectangle.h | 24 ++++++++++++++ src/graphics/draw.cc | 5 +++ 4 files changed, 105 insertions(+) create mode 100644 src/draw/rectangle.cc create mode 100644 src/draw/rectangle.h diff --git a/src/commands.h b/src/commands.h index 6c182420c..ce8d56f3d 100644 --- a/src/commands.h +++ b/src/commands.h @@ -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)}, diff --git a/src/draw/rectangle.cc b/src/draw/rectangle.cc new file mode 100644 index 000000000..214a87600 --- /dev/null +++ b/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 + +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 + diff --git a/src/draw/rectangle.h b/src/draw/rectangle.h new file mode 100644 index 000000000..cc6cbc084 --- /dev/null +++ b/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 + diff --git a/src/graphics/draw.cc b/src/graphics/draw.cc index d926f262b..d2fa66860 100644 --- a/src/graphics/draw.cc +++ b/src/graphics/draw.cc @@ -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;