alignment

This commit is contained in:
Gavin McDonald
2025-08-21 17:56:06 -04:00
parent 6baaa152ed
commit 677af45c73
3 changed files with 19 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ defmodule LabelmakerWeb.Constants do
@defaults %{ @defaults %{
color: "black", color: "black",
font: "Helvetica", font: "Helvetica",
gravity: "Center", align: "center",
height: "", height: "",
label: "", label: "",
label_too_long: false, label_too_long: false,
@@ -23,6 +23,12 @@ defmodule LabelmakerWeb.Constants do
|> Map.keys() |> Map.keys()
|> Enum.map(&Atom.to_string/1) |> Enum.map(&Atom.to_string/1)
@alignments [
"west",
"center",
"east"
]
@colors [ @colors [
"black", "black",
"blue", "blue",
@@ -85,6 +91,7 @@ defmodule LabelmakerWeb.Constants do
def max_label_length, do: @max_label_length def max_label_length, do: @max_label_length
def max_label_error, do: @max_label_error def max_label_error, do: @max_label_error
def outlines, do: ["none" | @colors] def outlines, do: ["none" | @colors]
def permitted_alignments, do: @alignments
def permitted_keys, do: @permitted_keys def permitted_keys, do: @permitted_keys
def preview, do: @preview def preview, do: @preview
def sizes, do: @sizes def sizes, do: @sizes

View File

@@ -51,11 +51,11 @@ defmodule LabelmakerWeb.LabelController do
] ]
end end
defp size_settings(args, %{gravity: gravity, height: height, width: width} = options) do defp size_settings(args, %{align: align, height: height, width: width} = options) do
args ++ args ++
[ [
"-gravity", "-gravity",
gravity, align,
"-size", "-size",
"#{width}x#{height}", "#{width}x#{height}",
"caption:#{String.slice(options.label, 0, Constants.max_label_length())}" "caption:#{String.slice(options.label, 0, Constants.max_label_length())}"

View File

@@ -20,6 +20,9 @@ defmodule LabelmakerWeb.Tools do
|> Map.take(Constants.permitted_keys()) |> Map.take(Constants.permitted_keys())
|> Map.new(fn {k, v} -> {String.to_atom(k), v} end) |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
|> Enum.map(fn |> Enum.map(fn
{:align, align} ->
{:align, process_alignment(align |> String.downcase())}
{:font, font} -> {:font, font} ->
{:font, Constants.font_map()[String.downcase(font)]} {:font, Constants.font_map()[String.downcase(font)]}
@@ -48,6 +51,7 @@ defmodule LabelmakerWeb.Tools do
pair pair
end) end)
|> Enum.filter(fn |> Enum.filter(fn
{:align, align} -> align in Constants.permitted_alignments()
{:color, color} -> color in Constants.colors() {:color, color} -> color in Constants.colors()
{:font, font} -> font in Map.values(Constants.font_map()) {:font, font} -> font in Map.values(Constants.font_map())
{:outline, outline} -> outline in Constants.outlines() {:outline, outline} -> outline in Constants.outlines()
@@ -59,6 +63,11 @@ defmodule LabelmakerWeb.Tools do
Map.merge(Constants.defaults(), parameters) Map.merge(Constants.defaults(), parameters)
end end
defp process_alignment("left"), do: "west"
defp process_alignment("middle"), do: "center"
defp process_alignment("right"), do: "east"
defp process_alignment(alignment), do: alignment |> String.downcase()
defp process_height("", parameters) do defp process_height("", parameters) do
parameters["width"] |> String.to_integer() |> max(0) |> min(Constants.max_height()) parameters["width"] |> String.to_integer() |> max(0) |> min(Constants.max_height())
end end