defmodule AprsmeWeb.AprsSymbol do @moduledoc """ Shared library for APRS symbol handling and rendering. This module provides centralized functions for: - Symbol table and code normalization - Sprite file mapping - Symbol positioning calculations - HTML generation for symbols All APRS symbol logic should use this module to ensure consistency across the application. """ @doc """ Gets sprite information for a given symbol table and code. Returns a map with sprite_file, background_position, and background_size. ## Examples iex> AprsmeWeb.AprsSymbol.get_sprite_info("/", "_") %{ sprite_file: "/aprs-symbols/aprs-symbols-128-0@2x.png", background_position: "-352px -32px", background_size: "512px 192px" } """ @spec get_sprite_info(String.t() | nil, String.t() | nil) :: sprite_info() def get_sprite_info(symbol_table, symbol_code) do compute_sprite_info(overlay_symbol?(symbol_table), symbol_table, symbol_code) end @typedoc "Sprite-sheet positioning information for a single symbol." @type sprite_info :: %{ sprite_file: String.t(), background_position: String.t(), background_size: String.t() } @spec overlay_symbol?(any()) :: boolean() defp overlay_symbol?(table) when is_binary(table), do: String.match?(table, ~r/^[A-Z0-9]$/) defp overlay_symbol?(_), do: false @spec compute_sprite_info(boolean(), String.t() | nil, String.t() | nil) :: sprite_info() defp compute_sprite_info(true, _symbol_table, symbol_code) do get_overlay_base_symbol_info(symbol_code) end defp compute_sprite_info(false, symbol_table, symbol_code) do symbol_table = normalize_symbol_table(symbol_table) symbol_code = normalize_symbol_code(symbol_code) table_id = get_table_id(symbol_table) build_sprite_info(table_id, symbol_code) end # Builds the sprite_info map for a given table and symbol char. # The 128x128 sprite sheet is a 16-column grid; positions are scaled to 32px display. @spec build_sprite_info(String.t(), String.t() | nil) :: sprite_info() defp build_sprite_info(table_id, symbol_char) do safe_index = symbol_char |> get_symbol_code_ord() |> Kernel.-(33) |> max(0) |> min(93) x = -rem(safe_index, 16) * 128 y = -div(safe_index, 16) * 128 %{ sprite_file: "/aprs-symbols/aprs-symbols-128-#{table_id}@2x.png", background_position: "#{x / 4}px #{y / 4}px", background_size: "512px 192px" } end @doc """ Gets sprite information for overlay symbols (A-Z, 0-9). These symbols display the base symbol from the overlay table. """ @spec get_overlay_base_symbol_info(String.t()) :: sprite_info() def get_overlay_base_symbol_info(base_symbol_code) do build_sprite_info(get_overlay_base_table_id(base_symbol_code), base_symbol_code) end @doc """ Determines which sprite table to use for overlay base symbols. Some symbols are in the alternate table (1), others in overlay table (2). """ @spec get_overlay_base_table_id(String.t()) :: String.t() def get_overlay_base_table_id(_base_symbol_code) do # All overlay symbols are in the alternate table (1) per APRS specification # This includes digipeaters, diamonds, squares, arrows, etc. "1" end @doc """ Gets sprite information for overlay characters (A-Z, 0-9). These are rendered from the overlay table. """ @spec get_overlay_character_sprite_info(String.t()) :: sprite_info() def get_overlay_character_sprite_info(overlay_char), do: build_sprite_info("2", overlay_char) @doc """ Normalizes a symbol table identifier. ## Examples iex> AprsmeWeb.AprsSymbol.normalize_symbol_table("/") "/" iex> AprsmeWeb.AprsSymbol.normalize_symbol_table("A") "]" iex> AprsmeWeb.AprsSymbol.normalize_symbol_table("invalid") "/" """ @spec normalize_symbol_table(String.t() | nil) :: String.t() def normalize_symbol_table(table) when table in ["/", "\\", "]"], do: table def normalize_symbol_table(table) when is_binary(table) do # A single alphanumeric character is an overlay — map to the overlay table. if String.match?(table, ~r/^[A-Z0-9]$/) do "]" else "/" end end def normalize_symbol_table(_), do: "/" @doc """ Normalizes a symbol code. ## Examples iex> AprsmeWeb.AprsSymbol.normalize_symbol_code("_") "_" iex> AprsmeWeb.AprsSymbol.normalize_symbol_code(nil) ">" iex> AprsmeWeb.AprsSymbol.normalize_symbol_code("") ">" """ @spec normalize_symbol_code(String.t() | nil) :: String.t() def normalize_symbol_code(nil), do: ">" def normalize_symbol_code(""), do: ">" def normalize_symbol_code(symbol_code), do: symbol_code @doc """ Maps a symbol table to its sprite file ID. ## Examples iex> AprsmeWeb.AprsSymbol.get_table_id("/") "0" iex> AprsmeWeb.AprsSymbol.get_table_id("\\") "1" iex> AprsmeWeb.AprsSymbol.get_table_id("]") "2" """ @spec get_table_id(String.t()) :: String.t() def get_table_id("/"), do: "0" def get_table_id("\\"), do: "1" def get_table_id("]"), do: "2" def get_table_id(_), do: "0" @doc """ Renders an APRS symbol as HTML for use in Leaflet markers. Returns HTML string that can be used as marker content. ## Examples iex> AprsmeWeb.AprsSymbol.render_marker_html("/", "_", "W1AW") "