Commit a9caad61 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add a new and improved syntax for drawing styles

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.
parent 3769cd15
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -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);

@@ -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;
+5 −0
Original line number Diff line number Diff line
@@ -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,

src/draw/path.cc

0 → 100644
+33 −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.
 */
#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

src/draw/path.h

0 → 100644
+37 −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 <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
+36 −34
Original line number Diff line number Diff line
@@ -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>

@@ -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
Loading