Commit 6e30975c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the 'hatched' fill style

parent 50381038
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
@@ -41,11 +41,22 @@ void fillPath(
  layer->apply(op);
}

void fillPath(
    Layer* layer,
    const Polygon2& poly,
    const Color& color) {
  layer_ops::BrushFillOp op;
  op.path = path_from_polygon(poly);
  op.color = color;

  layer->apply(op);
}

ReturnCode fillPath(
    Layer* layer,
    const Path& path,
    const FillStyle& style) {
  return style(path, layer);
  return style(path_to_polygon_simple(path), layer);
}

ReturnCode fillPath(
@@ -53,7 +64,7 @@ ReturnCode fillPath(
    const Rectangle& clip,
    const Path& path,
    const FillStyle& style) {
  return style(path, layer);
  return style(path_to_polygon_simple(path), layer);
}

void strokePath(
@@ -113,10 +124,10 @@ void strokeRectangle(
    double height,
    const StrokeStyle& style) {
  Path p;
  p.moveTo(origin.x, origin.y);
  p.lineTo(origin.x + width, origin.y);
  p.moveTo(origin.x,         origin.y + height);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x, origin.y + height);
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x,         origin.y);
  p.closePath();

  Rectangle clip(0, 0, layer->width, layer->height);
@@ -130,10 +141,10 @@ void fillRectangle(
    double height,
    const FillStyle& style) {
  Path p;
  p.moveTo(origin.x, origin.y);
  p.lineTo(origin.x + width, origin.y);
  p.moveTo(origin.x,         origin.y + height);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x, origin.y + height);
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x,         origin.y);
  p.closePath();

  Rectangle clip(0, 0, layer->width, layer->height);
+5 −0
Original line number Diff line number Diff line
@@ -46,6 +46,11 @@ void fillPath(
    const Path& path,
    const Color& color);

void fillPath(
    Layer* layer,
    const Polygon2& poly,
    const Color& color);

void fillPath(
    Layer* layer,
    const Rectangle& clip,
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include "text.h"
#include "text_layout.h"
#include "style.h"
#include "polygon.h"

namespace fviz {
namespace layer_ops {
+14 −2
Original line number Diff line number Diff line
@@ -105,6 +105,19 @@ std::string svg_path_data(const Path& path) {
  return path_data.str();
}

std::string svg_poly_data(const Polygon2& poly) {
  if (poly.vertices.empty()) {
    return "";
  }

  std::stringstream poly_data;
  for (const auto& v : poly.vertices) {
    poly_data << fmt::format("{} {} ", v.x, v.y);
  }

  return poly_data.str();
}

Status svg_stroke_path(
    const layer_ops::BrushStrokeOp& op,
    SVGDataRef svg) {
@@ -146,14 +159,13 @@ Status svg_fill_path(
    const layer_ops::BrushFillOp& op,
    SVGDataRef svg) {
  const auto& clip = op.clip;
  const auto& path = op.path;
  const auto& color = op.color;

  svg->buffer
      << "  "
      << "<path"
      << svg_attr("fill", color.to_hex_str())
      << svg_attr("d", svg_path_data(path))
      << svg_attr("d", svg_path_data(op.path))
      << "/>"
      << "\n";

+39 −0
Original line number Diff line number Diff line
@@ -141,4 +141,43 @@ bool Path::empty() const {
  return data_.size() == 0;
}

Polygon2 path_to_polygon_simple(const Path& path) {
  Polygon2 poly;

  for (size_t i = 0; i < path.size(); ++i) {
    const auto& cmd = path[i];

    switch (cmd.command) {
      case PathCommand::MOVE_TO:
      case PathCommand::LINE_TO:
        poly.vertices.emplace_back(cmd[0], cmd[1]);
        break;
      case PathCommand::QUADRATIC_CURVE_TO:
      case PathCommand::CUBIC_CURVE_TO:
      case PathCommand::ARC_TO:
      case PathCommand::CLOSE:
        continue;
    }
  }

  return poly;
}

Path path_from_polygon(const Polygon2& poly) {
  Path p;

  if (poly.vertices.empty()) {
    return p;
  }

  p.moveTo(poly.vertices[0].x, poly.vertices[0].y);

  for (size_t i = 1; i < poly.vertices.size(); ++i) {
    p.lineTo(poly.vertices[i].x, poly.vertices[i].y);
  }

  p.closePath();
  return p;
}

} // namespace fviz
Loading