widthxheight support

This commit is contained in:
Gavin McDonald
2025-08-18 20:24:24 -04:00
parent 7fc9736cdc
commit 8baa70485f
3 changed files with 123 additions and 44 deletions

View File

@@ -8,37 +8,33 @@ defmodule LabelmakerWeb.Tools do
alias LabelmakerWeb.Constants
def process_parameters(parameters) do
# ensure our defaults are in place to cover any missing parameters
%{"label" => label, "size" => size} =
parameters =
Constants.defaults()
|> Map.new(fn {k, v} -> {Atom.to_string(k), v} end)
|> Map.merge(parameters)
link = ~p"/#{label}?#{Map.take(parameters, ["color", "font", "outline", "size"])}"
line_breaks = Regex.scan(~r/#{Regex.escape("\\n")}/, label) |> length()
size = String.to_integer(size)
parameters =
parameters
|> Map.take(Constants.permitted_keys())
|> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
|> Enum.map(fn
{:font, font} ->
{:font, Constants.font_map()[String.downcase(font)]}
{:height, height} ->
{:height, process_height(height, parameters)}
{:label, label} ->
if String.length(label) > Constants.max_label_length(),
do: {:label, String.slice(label, 0, Constants.max_label_length() + 1)},
else: {:label, label}
{:label, process_label(label)}
{:link, _} ->
{:link, link}
{:link, generate_link(parameters)}
{:preview_height, _} ->
{:preview_height, size + size * line_breaks}
{:preview_height, calculate_preview_height(parameters)}
{:preview_text, _} ->
{:preview_text, String.split(label, "\\n")}
{:preview_text, String.split(parameters.label, "\\n")}
{:width, width} ->
{:width, process_width(width, parameters)}
pair ->
pair
@@ -55,6 +51,51 @@ defmodule LabelmakerWeb.Tools do
Map.merge(Constants.defaults(), parameters)
end
defp process_height("0", parameters) do
parameters.width |> String.to_integer() |> max(0) |> min(Constants.max_height())
end
defp process_height(height, _parameters) do
height |> String.to_integer() |> max(0) |> min(Constants.max_height())
end
defp process_label(label) do
if String.length(label) > Constants.max_label_length() do
String.slice(label, 0, Constants.max_label_length())
else
label
end
end
defp generate_link(%{:height => "0", "label" => label, :width => "0"} = parameters) do
~p"/#{label}?#{Map.take(parameters, ["color", "font", "outline", "size"])}"
end
defp generate_link(%{height: "0", width: width} = parameters),
do: generate_link(%{parameters | height: width, width: width})
defp generate_link(%{height: height, width: "0"} = parameters),
do: generate_link(%{parameters | height: height, width: height})
defp generate_link(%{"label" => label} = parameters) do
~p"/#{label}?#{Map.take(parameters, ["color", "font", "height", "outline", "width"])}"
end
defp process_width("0", parameters) do
parameters.height |> String.to_integer() |> max(0) |> min(Constants.max_width())
end
defp process_width(width, _parameters) do
width |> String.to_integer() |> max(0) |> min(Constants.max_width())
end
defp calculate_preview_height(parameters) do
size = parameters.size |> String.to_integer()
line_breaks = Regex.scan(~r/#{Regex.escape("\\n")}/, parameters.label) |> length()
size + size * line_breaks
end
def outline(_, error: true), do: outline(Constants.danger(), false)
def outline("none", _error), do: ""