defmodule DataMatrix.SVG do @default_options %{ background: "white", color: "black" } def format(%{nrow: nrow, ncol: ncol, matrix: rows}, options \\ []) do options = Map.merge(@default_options, Map.new(options)) points = rows |> Stream.with_index(0) |> Stream.flat_map(fn {row, index} -> row |> Stream.chunk_by(& &1) |> Enum.map(&Enum.count/1) |> prepend(0) |> Stream.scan(&(&1 + &2)) |> Stream.flat_map(fn x -> [x, x] end) |> Stream.drop(1) |> Stream.drop(-1) |> Stream.chunk_every(2) |> Stream.zip(Stream.dedup(row)) |> Stream.map(fn {[start, stop], module} -> "#{start},#{index + 1 - module} #{stop},#{index + 1 - module}" end) |> Stream.concat(["#{ncol},#{index + 1} 0,#{index + 1}"]) end) |> Enum.join(" ") ~s""" """ end defp prepend(list, element) do [element | list] end end