Commit 72b940db authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the 'dashed' stroke style

parent 2b4235d8
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ void strokeRectangle(
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x, origin.y + height);
  p.lineTo(origin.x, origin.y);
  p.closePath();

  Rectangle clip(0, 0, layer->width, layer->height);
  strokePath(layer, clip, p, style);
@@ -134,7 +134,7 @@ void fillRectangle(
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x, origin.y + height);
  p.lineTo(origin.x, origin.y);
  p.closePath();

  Rectangle clip(0, 0, layer->width, layer->height);
  fillPath(layer, clip, p, style);
@@ -151,7 +151,7 @@ void fillRectangle(
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x, origin.y + height);
  p.lineTo(origin.x, origin.y);
  p.closePath();

  Rectangle clip(0, 0, layer->width, layer->height);
  fillPath(layer, clip, p, color);
+16 −1
Original line number Diff line number Diff line
@@ -112,6 +112,21 @@ Status svg_stroke_path(
  const auto& path = op.path;
  const auto& style = op.style;

  std::string dash_opts;
  switch (style.dash_type) {
    case StrokeStyle::SOLID:
      break;
    case StrokeStyle::DASH: {
      std::string dash_pattern;
      for (const auto& v : style.dash_pattern) {
        dash_pattern += fmt::format("{} ", v);
      }

      dash_opts = svg_attr("stroke-dasharray", dash_pattern);
      break;
    }
  }

  svg->buffer
      << "  "
      << "<path"
@@ -119,6 +134,7 @@ Status svg_stroke_path(
      << svg_attr("stroke", style.color.to_hex_str())
      << svg_attr("fill", "none")
      << svg_attr("d", svg_path_data(path))
      << dash_opts
      << "/>"
      << "\n";

@@ -259,7 +275,6 @@ ReturnCode layer_bind_svg(
      << "/>"
      << "\n";


  LayerRef l(new Layer());
  l->width = svg->width = width,
  l->height = svg->height = height,
+21 −0
Original line number Diff line number Diff line
@@ -66,6 +66,9 @@ Status Rasterizer::fillPath(const layer_ops::BrushFillOp& op) {
      case PathCommand::ARC_TO:
        cairo_arc(cr_ctx, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
        break;
      case PathCommand::CLOSE:
        cairo_close_path(cr_ctx);
        break;
      default:
        break; // not yet implemented
    }
@@ -94,6 +97,21 @@ Status Rasterizer::strokePath(const layer_ops::BrushStrokeOp& op) {

  cairo_set_line_width(cr_ctx, style.line_width);

  switch (style.dash_type) {
    case StrokeStyle::SOLID:
      cairo_set_dash(cr_ctx, nullptr, 0, 0);
      break;
    case StrokeStyle::DASH: {
      std::vector<double> dash_pattern;
      for (const auto& v : style.dash_pattern) {
        dash_pattern.push_back(v);
      }

      cairo_set_dash(cr_ctx, dash_pattern.data(), dash_pattern.size(), 0);
      break;
    }
  }

  cairo_new_path(cr_ctx);

  for (const auto& cmd : path) {
@@ -107,6 +125,9 @@ Status Rasterizer::strokePath(const layer_ops::BrushStrokeOp& op) {
      case PathCommand::ARC_TO:
        cairo_arc(cr_ctx, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
        break;
      case PathCommand::CLOSE:
        cairo_close_path(cr_ctx);
        break;
      default:
        break; // not yet implemented
    }
+2 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ FillStyle fill_style_none();
struct StrokeStyle {
  enum DashType {
    SOLID,
    DASH
  };

  StrokeStyle() :
@@ -55,6 +56,7 @@ struct StrokeStyle {
  Measure line_width;
  Color color;
  DashType dash_type;
  std::vector<Measure> dash_pattern;
};


+11 −0
Original line number Diff line number Diff line
@@ -21,8 +21,13 @@ using namespace std::placeholders;
namespace fviz {

ReturnCode stroke_style_read(
    const Environment& env,
    const Expr* expr,
    StrokeStyle* style) {
  if (expr_is_list(expr)) {
    expr = expr_get_list(expr);
  }

  if (expr_is_value(expr, "none")) {
    style->line_width = from_unit(0);
    return OK;
@@ -33,6 +38,12 @@ ReturnCode stroke_style_read(
    return OK;
  }

  if (expr_is_value(expr, "dash") || expr_is_value(expr, "dashed")) {
    style->dash_type = StrokeStyle::DASH;
    style->dash_pattern = {from_pt(2, env.dpi), from_pt(2, env.dpi)};
    return OK;
  }

  return errorf(
      ERROR,
      "invalid <stroke-style>; expected one of (none, solid), "
Loading