Commit 2e8a328c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

cleanup the low-level drawing API

parent 24c0020c
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -25,7 +25,8 @@ ReturnCode arrow_draw_default(
    const Point& to,
    const Measure& size,
    const Color& color,
    Page* layer) {
    const Page& page,
    PageElementList* page_elements) {
  auto direction = normalize(sub(to, from));
  auto ortho = vec2{direction.y, direction.x * -1};

@@ -41,14 +42,17 @@ ReturnCode arrow_draw_default(
  line_style.color = color;
  line_style.line_width = size;

  strokeLine(layer, from, to, line_style);
  fillPath(layer, head_path, color);
  FillStyle fill_style;
  fill_style.color = color;

  page_add_line(page_elements, from, to, line_style);
  page_add_path(page_elements, head_path, {}, fill_style);

  return OK;
}

Arrow arrow_create_default() {
  return bind(&arrow_draw_default, _1, _2, _3, _4, _5);
  return bind(&arrow_draw_default, _1, _2, _3, _4, _5, _6);
}

} // namespace clip
+2 −1
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ using Arrow = std::function<
        const Point& to,
        const Measure& size,
        const Color& color,
        Page* page)>;
        const Page& page,
        PageElementList* page_elements)>;

Arrow arrow_create_default();

+2 −1
Original line number Diff line number Diff line
@@ -36,7 +36,8 @@ using ElementConfigureFn = std::function<
using ElementDrawFn = std::function<
    ReturnCode (
        const LayoutInfo& layout,
        Page* page)>;
        const Page& page,
        PageElementList* page_elements)>;

using ElementSizeHintFn = std::function<
    ReturnCode (
+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ ReturnCode eval(

  // draw all elements
  for (const auto& e : roots) {
    if (auto rc = e->draw(layout, &page); !rc) {
    if (auto rc = e->draw(layout, page, &page.elements); !rc) {
      return rc;
    }
  }
+0 −152
Original line number Diff line number Diff line
@@ -17,157 +17,5 @@

namespace clip {

void fillPath(
    Page* page,
    const Path& path,
    const Color& color) {
  fillPath(
      page,
      Rectangle(0, 0, page->width, page->height),
      path,
      color);
}

void fillPath(
    Page* page,
    const Rectangle& clip,
    const Path& path,
    const Color& color) {
  PageShapeElement elem;
  elem.clip = clip;
  elem.path = path;
  elem.fill_color = color;

  page_add_shape(page, elem);
}

void fillPath(
    Page* page,
    const Polygon2& poly,
    const Color& color) {
  PageShapeElement elem;
  elem.path = path_from_polygon(poly);
  elem.fill_color = color;

  page_add_shape(page, elem);
}

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

ReturnCode fillPath(
    Page* page,
    const Rectangle& clip,
    const Path& path,
    const FillStyle& style) {
  return style(path_to_polygon_simple(path), page);
}

void strokePath(
    Page* page,
    const Path& path,
    const StrokeStyle& style) {
  strokePath(
      page,
      Rectangle(0, 0, page->width, page->height),
      path.data(),
      path.size(),
      style);
}

void strokePath(
    Page* page,
    const Rectangle& clip,
    const Path& path,
    const StrokeStyle& style) {
  if (style.line_width == 0) {
    return;
  }

  PageShapeElement elem;
  elem.clip = clip;
  elem.path = path;
  elem.stroke_style = style;

  page_add_shape(page, elem);
}

void strokePath(
    Page* page,
    const Rectangle& clip,
    const PathData* point_data,
    size_t point_count,
    const StrokeStyle& style) {
  strokePath(page, clip, Path(point_data, point_count), style);
}

void strokeLine(
    Page* page,
    const Point& p1,
    const Point& p2,
    const StrokeStyle& style) {
  Path p;
  p.moveTo(p1.x, p1.y);
  p.lineTo(p2.x, p2.y);

  Rectangle clip(0, 0, page->width, page->height);
  strokePath(page, clip, p, style);
}

void strokeRectangle(
    Page* page,
    const Point& origin,
    double width,
    double height,
    const StrokeStyle& style) {
  Path p;
  p.moveTo(origin.x,         origin.y + height);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x,         origin.y);
  p.closePath();

  Rectangle clip(0, 0, page->width, page->height);
  strokePath(page, clip, p, style);
}

void fillRectangle(
    Page* page,
    const Point& origin,
    double width,
    double height,
    const FillStyle& style) {
  Path p;
  p.moveTo(origin.x,         origin.y + height);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x,         origin.y);
  p.closePath();

  Rectangle clip(0, 0, page->width, page->height);
  fillPath(page, clip, p, style);
}

void fillRectangle(
    Page* page,
    const Point& origin,
    double width,
    double height,
    const Color& color) {
  Path p;
  p.moveTo(origin.x, origin.y);
  p.lineTo(origin.x + width, origin.y);
  p.lineTo(origin.x + width, origin.y + height);
  p.lineTo(origin.x, origin.y + height);
  p.closePath();

  Rectangle clip(0, 0, page->width, page->height);
  fillPath(page, clip, p, color);
}

} // namespace clip
Loading