#include "encoder_decoder.hpp" namespace utf4088 { std::optional pack_input(const InputFields& f) { if (f.x > 7 || f.y > 11 || f.pressure < 1 || f.pressure > 50) { return std::nullopt; } return static_cast(f.x) | (static_cast(f.y) << 3) | (static_cast(f.pressure - 1) << 7) | (static_cast(f.voltage) << 13); } std::optional unpack_input(std::uint32_t packed) { if ((packed & ~kPackedInputMask) != 0) return std::nullopt; InputFields f{ static_cast(packed & 0x7U), static_cast((packed >> 3) & 0xFU), static_cast(((packed >> 7) & 0x3FU) + 1U), static_cast((packed >> 13) & 0xFFU) }; if (f.y > 11 || f.pressure > 50) return std::nullopt; return f; } std::optional encode_input(const InputFields& fields) { const auto packed = pack_input(fields); if (!packed) return std::nullopt; return kInputIdentifierBase + *packed; } std::optional decode_input(CodePoint identifier) { if (identifier < kInputIdentifierBase) return std::nullopt; const auto delta = identifier - kInputIdentifierBase; if (delta > kPackedInputMask) return std::nullopt; return unpack_input(static_cast(delta)); } } // namespace utf4088