Commit 7a4c160e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

slightly cleaner svg layer

parent 5d079e34
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -168,6 +168,7 @@ ReturnCode document_render_svg(
      to_px(doc.measures, doc.width).value,
      to_px(doc.measures, doc.height).value,
      doc.measures,
      doc.background_colour,
      [filename] (auto svg) {
        FileUtil::write(filename, Buffer(svg.data(), svg.size()));
        return OK;
+28 −17
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ struct SVGData {
  std::stringstream buffer;
  uint32_t width;
  uint32_t height;
  Colour background_colour;
  std::string to_svg() const;
};

@@ -75,10 +76,13 @@ Status svg_stroke_path(
    return ERROR_INVALID_ARGUMENT;
  }

  svg->buffer << StringUtil::format(
      "  <path stroke-width='$0' stroke='$1' fill='none' d=\"",
      to_px(measures, style.line_width).value,
      style.colour.to_hex_str());
  svg->buffer
      << "  "
      << "<path"
      << " stroke-width='" << to_px(measures, style.line_width).value << "'"
      << " stroke='" << style.colour.to_hex_str() << "'"
      << " fill='none'"
      << " d=\"";

  for (const auto& cmd : path) {
    switch (cmd.command) {
@@ -94,25 +98,32 @@ Status svg_stroke_path(
  }

  svg->buffer << "\"/>\n";

  return OK;
}

std::string SVGData::to_svg() const {
  return StringUtil::format(
      "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox='0 0 $0 $1' viewport-fill='white'>\n$2\n</svg>",
      width,
      height,
      buffer.str());
  std::stringstream svg;
  svg
    << "<svg xmlns=\"http://www.w3.org/2000/svg\""
    << " viewBox='0 0 " << width << " " << height << "'"
    << " viewport-fill='" << background_colour.to_hex_str() << "'"
    << ">\n"
    << buffer.str()
    << "</svg>";

  return svg.str();
}

ReturnCode layer_bind_svg(
    double width,
    double height,
    const MeasureTable& measures,
    const Colour& background_colour,
    std::function<Status (const std::string&)> submit,
    LayerRef* layer) {
  auto svg = std::make_shared<SVGData>();
  svg->background_colour = background_colour;

  layer->reset(new Layer{
    .width = svg->width = width,
    .height = svg->height = height,
+1 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ ReturnCode layer_bind_svg(
    double width,
    double height,
    const MeasureTable& measures,
    const Colour& background_colour,
    std::function<Status (const std::string&)> submit,
    LayerRef* layer);