Commit 33abd098 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

improved backend drawing interface using std::variant

parent 47999da2
Loading
Loading
Loading
Loading
+4 −9
Original line number Diff line number Diff line
@@ -50,14 +50,12 @@ void fillPath(
    const Rectangle& clip,
    const Path& path,
    const FillStyle& style) {
  BrushFillOp op;
  layer_ops::BrushFillOp op;
  op.clip = clip;
  op.path = path;
  op.style = style;

  if (layer->op_brush_fill) {
    layer->op_brush_fill(op);
  }
  layer->apply(op);
}

void fillPath(
@@ -86,14 +84,11 @@ void strokePath(
    const Rectangle& clip,
    const Path& path,
    const StrokeStyle& style) {
  BrushStrokeOp op;
  layer_ops::BrushStrokeOp op;
  op.clip = clip;
  op.path = path;
  op.style = style;

  if (layer->op_brush_stroke) {
    layer->op_brush_stroke(op);
  }
  layer->apply(op);
}

void strokePath(
+0 −12
Original line number Diff line number Diff line
@@ -62,18 +62,6 @@ struct FillStyle {
  Colour colour;
};

struct BrushStrokeOp {
  Rectangle clip;
  Path path;
  StrokeStyle style;
};

struct BrushFillOp {
  Rectangle clip;
  Path path;
  FillStyle style;
};

void fillPath(
    Layer* layer,
    const Path& path,
+2 −5
Original line number Diff line number Diff line
@@ -36,9 +36,8 @@
#include "colour.h"
#include "plotfx.h"
#include "text_shaper.h"
#include "rasterize.h"
#include "image.h"
#include "measure.h"
#include "layer_ops.h"

namespace plotfx {

@@ -47,9 +46,7 @@ struct Layer {
  const double height;
  const MeasureTable measures;
  const std::shared_ptr<text::TextShaper> text_shaper;
  const std::function<Status (const BrushStrokeOp&)> op_brush_stroke;
  const std::function<Status (const BrushFillOp&)> op_brush_fill;
  const std::function<Status (const TextSpanOp&)> op_text_span;
  const std::function<Status (const layer_ops::Op&)> apply;
  const std::function<std::string ()> data;
};

+71 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
#include <stdlib.h>
#include <vector>
#include <string>
#include <variant>

#include "colour.h"
#include "measure.h"
#include "brush.h"
#include "text.h"

namespace plotfx {
namespace layer_ops {

struct BrushStrokeOp {
  Rectangle clip;
  Path path;
  StrokeStyle style;
};

struct BrushFillOp {
  Rectangle clip;
  Path path;
  FillStyle style;
};

struct TextSpanOp {
  std::string text;
  double x;
  double y;
  TextStyle style;
};

using Op = std::variant<
    BrushFillOp,
    BrushStrokeOp,
    TextSpanOp>;

} // namespace layer_ops
} // namespace plotfx
+14 −12
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "layer_pixmap.h"
#include "rasterize.h"

namespace plotfx {

@@ -46,18 +47,19 @@ ReturnCode layer_bind_png(
    .height = height,
    .measures = measures,
    .text_shaper = text_shaper,
    .op_brush_stroke = std::bind(
        &Rasterizer::strokePath,
        raster.get(), // safe
        std::placeholders::_1),
    .op_brush_fill = std::bind(
        &Rasterizer::fillPath,
        raster.get(), // safe
        std::placeholders::_1),
    .op_text_span = std::bind(
        &Rasterizer::drawText,
        raster.get(), // safe
        std::placeholders::_1),
    .apply = [raster] (auto op) {
      return std::visit([raster] (auto&& op) {
        using T = std::decay_t<decltype(op)>;
        if constexpr (std::is_same_v<T, layer_ops::BrushStrokeOp>)
          return raster->strokePath(op);
        if constexpr (std::is_same_v<T, layer_ops::BrushFillOp>)
          return raster->fillPath(op);
        if constexpr (std::is_same_v<T, layer_ops::TextSpanOp>)
          return raster->drawText(op);
        else
          return ERROR_NOT_IMPLEMENTED;
      }, op);
    },
    .data = [raster] { return raster->to_png(); },
  });

Loading