Skip to content

Commit

Permalink
add a new and improved syntax for drawing styles
Browse files Browse the repository at this point in the history
The new syntax is more consistent and allows users to specify
multiple stroke/fill types per draw command.

This change also introduces new drawing style structs. Note that
the new type names use snake case and don't follow the previous
naming convention of using capitalized names for types. This will
eventually be cleaned up by either changing everything to snake
case or by capitalizing the new type names.
  • Loading branch information
asmuth committed May 22, 2020
1 parent 3769cd1 commit a9caad6
Show file tree
Hide file tree
Showing 53 changed files with 834 additions and 142 deletions.
13 changes: 9 additions & 4 deletions src/color_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ ReturnCode color_read(
const Context* ctx,
const Expr* expr,
Color* color) {
const auto layer = layer_get(ctx);
return expr_to_color(expr, *layer_get(ctx), color);
}

ReturnCode expr_to_color(
const Expr* expr,
const Layer& layer,
Color* color) {
if (expr_is_value(expr)) {
const auto value = expr_get_value(expr);

Expand All @@ -38,9 +43,9 @@ ReturnCode color_read(

// color palette index
if (StringUtil::isDigitString(value)) {
if (!layer->color_palette.empty()) {
*color = layer->color_palette[
(std::stol(value) - 1) % layer->color_palette.size()];
if (!layer.color_palette.empty()) {
*color = layer.color_palette[
(std::stol(value) - 1) % layer.color_palette.size()];
}

return OK;
Expand Down
5 changes: 5 additions & 0 deletions src/color_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ ReturnCode color_read(
const Expr* expr,
Color* color);

ReturnCode expr_to_color(
const Expr* expr,
const Layer& layer,
Color* color);

ReturnCode color_read_string(
const Context* ctx,
const std::string& value,
Expand Down
33 changes: 33 additions & 0 deletions src/draw/path.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.
*/
#include "path.h"
#include "style_util.h"

namespace clip::draw {

ReturnCode path(const path_op& op, Layer* layer) {
DrawCommand shape;
shape.path = op.path;
shape.style = op.style;

if (!style_is_visible(shape.style)) {
shape.style = layer->draw_default_style;
}

layer->drawlist.emplace_back(std::move(shape));
return OK;
}

} // namespace clip::draw

37 changes: 37 additions & 0 deletions src/draw/path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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 <stdlib.h>
#include <string>
#include <vector>

#include "layer.h"
#include "graphics/path.h"
#include "style.h"

namespace clip::draw {

struct path_op {
Path path;
draw_style::compound style;
Option<AntialiasingMode> antialiasing_mode;
};

/**
* Draw a path
*/
ReturnCode path(const path_op& op, Layer* l);

} // namespace clip::draw

70 changes: 36 additions & 34 deletions src/draw/rectangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,10 @@
* limitations under the License.
*/
#include "rectangle.h"
#include "data.h"
#include "sexpr.h"
#include "draw/path.h"
#include "style_reader.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>

Expand All @@ -41,38 +29,52 @@ ReturnCode rectangle(
const Expr* expr) {
const auto layer = layer_get(ctx);

Rectangle rect;
rect.w = layer_get_width(*layer).value;
rect.h = layer_get_height(*layer).value;
/* read arguments */
draw_style::compound style;

FillStyle fill_style;
StrokeStyle stroke_style;
stroke_style.line_width = unit_from_pt(1, layer_get_dpi(ctx));
vec2 position = {
layer_get_width(*layer).value * .5,
layer_get_height(*layer).value * .5
};

vec2 size = {
layer_get_width(*layer).value,
layer_get_height(*layer).value
};

/* read arguments */
auto config_rc = expr_walk_map(expr, {
{
"color",
expr_calln_fn({
std::bind(&color_read, ctx, _1, &stroke_style.color),
std::bind(&fill_style_read_solid, ctx, _1, &fill_style),
})
"position",
std::bind(
&expr_to_vec2,
_1,
layer_get_uconv_width(*layer),
layer_get_uconv_height(*layer),
&position)
},
{
"size",
std::bind(
&expr_to_vec2,
_1,
layer_get_uconv_size(*layer),
layer_get_uconv_size(*layer),
&size)
},
{"fill", std::bind(&fill_style_read, ctx, _1, &fill_style)},
{"stroke-color", std::bind(&color_read, ctx, _1, &stroke_style.color)},
{"stroke-width", std::bind(&expr_to_size, _1, *layer, &stroke_style.line_width)},
{"stroke-style", std::bind(&stroke_style_read, ctx, _1, &stroke_style)},
{"fill", std::bind(&style_read_fill, _1, *layer, &style)},
{"stroke", std::bind(&style_read_stroke, _1, *layer, &style)},
});

if (!config_rc) {
return config_rc;
}

Path p;
path_add_rectangle(&p, rect);
draw_path(ctx, p, stroke_style, fill_style);
/* draw the rectangle */
draw::path_op op;
op.style = style;
path_add_rectangle(&op.path, position, size);

return OK;
return draw::path(op, layer);
}

} // namespace clip::draw
Expand Down
8 changes: 4 additions & 4 deletions src/draw/text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ ReturnCode text_draw(
auto offset = layout_align(
bbox,
{
placement.position[0].value,
placement.position[1].value,
placement.position.x,
placement.position.y,
},
placement.align_x,
placement.align_y);
Expand Down Expand Up @@ -113,8 +113,8 @@ ReturnCode text_draw(
style.font = layer_get_font(ctx);
style.font_size = layer_get_font_size(ctx);

placement.position[0] = layer_get_width(*layer) * .5f;
placement.position[1] = layer_get_height(*layer) * .5f;
placement.position[0] = layer_get_width(*layer).value * .5f;
placement.position[1] = layer_get_height(*layer).value * .5f;
placement.align_x = HAlign::CENTER;
placement.align_y = VAlign::CENTER;

Expand Down
2 changes: 1 addition & 1 deletion src/draw/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace clip::draw {

struct TextPlacement {
Vector2 position;
vec2 position;
HAlign align_x;
VAlign align_y;
};
Expand Down
12 changes: 0 additions & 12 deletions src/graphics/draw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,5 @@

namespace clip {

void draw_polygon(
Context* ctx,
const Poly2& poly,
StrokeStyle stroke_style,
FillStyle fill_style) {
DrawCommand elem;
elem.path = path_from_poly2(poly);
elem.stroke_style = stroke_style;
elem.fill_style = fill_style;
layer_get(ctx)->drawlist.push_back(elem);
}

} // namespace clip

64 changes: 62 additions & 2 deletions src/graphics/draw_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,56 @@

namespace clip {

void draw_shape(Context* ctx, DrawCommand elem) {
layer_get(ctx)->drawlist.emplace_back(std::move(elem));
void draw_shape(Context* ctx, DrawCommand shape) {
auto layer = layer_get(ctx);

if (shape.fill_style.color) {
if (shape.fill_style.hatch) {
shape.style.fill_hatch.push_back({
.color = *shape.fill_style.color,
.angle_deg = shape.fill_style.hatch_angle_deg,
.offset = Number(shape.fill_style.hatch_offset),
.stride = Number(shape.fill_style.hatch_stride),
.width = Number(shape.fill_style.hatch_width),
});
} else {
shape.style.fill_solid.push_back({
.color = *shape.fill_style.color
});
}

shape.fill_style = {};
}

if (shape.stroke_style.line_width.value) {
switch (shape.stroke_style.dash_type) {
case StrokeStyle::SOLID:
shape.style.stroke_solid.push_back({
.color = shape.stroke_style.color,
.width = shape.stroke_style.line_width
});
break;
case StrokeStyle::DASH:
std::vector<Number> pattern(shape.stroke_style.dash_pattern.size());
std::transform(
shape.stroke_style.dash_pattern.begin(),
shape.stroke_style.dash_pattern.end(),
pattern.begin(),
[] (auto v) { return Number(v); });

shape.style.stroke_dash.push_back({
.color = shape.stroke_style.color,
.width = shape.stroke_style.line_width,
.pattern = pattern,
.offset = Number(shape.stroke_style.dash_offset),
});
break;
}

shape.stroke_style = {};
}

layer_get(ctx)->drawlist.emplace_back(std::move(shape));
}

void draw_path(
Expand All @@ -36,6 +84,18 @@ void draw_path(
draw_shape(ctx, shape);
}

void draw_polygon(
Context* ctx,
const Poly2& poly,
StrokeStyle stroke_style,
FillStyle fill_style) {
DrawCommand shape;
shape.path = path_from_poly2(poly);
shape.stroke_style = stroke_style;
shape.fill_style = fill_style;
draw_shape(ctx, shape);
}

void draw_line(
Context* ctx,
vec2 from,
Expand Down
1 change: 1 addition & 0 deletions src/graphics/draw_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace clip {

struct DrawCommand {
Path path;
draw_style::compound style;
StrokeStyle stroke_style;
FillStyle fill_style;
Option<AntialiasingMode> antialiasing_mode;
Expand Down
Loading

0 comments on commit a9caad6

Please sign in to comment.