diff --git a/palm-client/Makefile b/palm-client/Makefile index 245e0ad..a3eeab7 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -10,7 +10,7 @@ APP = visormesh NAME = VisorMesh CREATOR = VMsh -SRCS = src/main.c src/protocol.c src/transport_srm.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/preview_form.c src/ui/draw.c src/ui/fontmetrics.c src/ports.c +SRCS = src/main.c src/protocol.c src/transport_srm.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/fontmetrics.c src/ports.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -55,6 +55,8 @@ build/$(APP).prc: build/$(APP) build/res.stamp test: | build gcc -std=c99 -Wall -Wextra -o build/test_protocol tests/test_protocol.c src/protocol.c build/test_protocol + gcc -std=c99 -Wall -Wextra -o build/test_text tests/test_text.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c + build/test_text build: mkdir -p build diff --git a/palm-client/src/preview_form.c b/palm-client/src/preview_form.c index 783042c..21c683f 100644 --- a/palm-client/src/preview_form.c +++ b/palm-client/src/preview_form.c @@ -42,12 +42,12 @@ static void DrawPreview(void) Row(y, GLYPH_SENSOR, "Weather Sensor", NULL, NULL, NULL); y += VM_ROW_H; DrawSection("Thu Nov 1", y); y += VM_ROW_H + 1; - DrawChip("Steve K.", 1, y); y += VM_ROW_H + 1; + DrawChip("Steve K.", 1, y); y += VM_CHIP_H + 1; WinDrawChars("11:30", 5, 4, y); WinDrawChars("I'll bring a spare antenna", 26, 26, y); y += VM_ROW_H; WinDrawChars("for anyone who needs one +6dB", 29, 26, y); y += VM_ROW_H + 1; StrPrintF(line, "%c VisorNeo", GLYPH_SELF); - DrawChip(line, 1, y); y += VM_ROW_H + 1; + DrawChip(line, 1, y); y += VM_CHIP_H + 1; WinDrawChars("11:33", 5, 4, y); StrPrintF(line, "on my way, caf%c at 3 %c", (Char)0xE9, GLYPH_ACKED); WinDrawChars(line, StrLen(line), 26, y); y += VM_ROW_H + 2; diff --git a/palm-client/src/protocol.h b/palm-client/src/protocol.h index 682718c..b09adfc 100644 --- a/palm-client/src/protocol.h +++ b/palm-client/src/protocol.h @@ -18,6 +18,8 @@ #include #else #include +#ifndef VM_HOST_TYPES +#define VM_HOST_TYPES typedef uint8_t UInt8; typedef uint16_t UInt16; typedef uint32_t UInt32; @@ -27,6 +29,7 @@ typedef int32_t Int32; typedef uint8_t Boolean; typedef char Char; #endif +#endif #define VM_FRAME_TO_RADIO 0x3C /* '<' */ #define VM_FRAME_FROM_RADIO 0x3E /* '>' */ diff --git a/palm-client/src/ui/draw.c b/palm-client/src/ui/draw.c index b88a223..dbef57b 100644 --- a/palm-client/src/ui/draw.c +++ b/palm-client/src/ui/draw.c @@ -41,15 +41,19 @@ void DrawSection(const Char *label, Coord y) FntSetFont(old); } +/* 2 px of black either side and 1 px above and below: inverted 4x6 text smears with less. */ +#define CHIP_PAD_X 2 +#define CHIP_PAD_Y 1 + Coord DrawChip(const Char *text, Coord x, Coord y) { UInt16 len = StrLen(text); - Coord w = FntCharsWidth(text, len) + 2; + Coord w = FntCharsWidth(text, len) + 2 * CHIP_PAD_X; RectangleType r; - RctSetRectangle(&r, x, y, w, FntLineHeight()); + RctSetRectangle(&r, x, y, w, FntLineHeight() + 2 * CHIP_PAD_Y); WinDrawRectangle(&r, 0); - WinDrawInvertedChars(text, len, x + 1, y); + WinDrawInvertedChars(text, len, x + CHIP_PAD_X, y + CHIP_PAD_Y); return w; } diff --git a/palm-client/src/ui/draw.h b/palm-client/src/ui/draw.h index 7adad78..a460487 100644 --- a/palm-client/src/ui/draw.h +++ b/palm-client/src/ui/draw.h @@ -18,7 +18,8 @@ void UiFontRelease(void); /* "── Label ──" across the full width at y, in the small font. */ void DrawSection(const Char *label, Coord y); -/* A name chip: text inverted (white on black) with a 1px pad. Returns its width. */ +/* A name chip: text inverted (white on black), VM_CHIP_H tall. Returns its width. */ +#define VM_CHIP_H (VM_SMALL_FONT_HEIGHT + 2) Coord DrawChip(const Char *text, Coord x, Coord y); /* Draws text in the current font, cut with an ellipsis to fit maxW. Returns the width drawn. */ diff --git a/palm-client/src/ui/text.c b/palm-client/src/ui/text.c new file mode 100644 index 0000000..5f9c5c7 --- /dev/null +++ b/palm-client/src/ui/text.c @@ -0,0 +1,175 @@ +#include "text.h" +#include "fontmetrics.h" + +Int16 TxtSmallWidth(Char c) +{ + return gSmallFontWidths[(UInt8)c]; +} + +Int16 TxtWidth(const Char *s, UInt16 len, TxtWidthFn w) +{ + Int16 total = 0; + UInt16 i; + + for (i = 0; i < len; i++) + total += w(s[i]); + return total; +} + +UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w) +{ + Int16 used = 0; + UInt16 i; + + for (i = 0; i < len; i++) { + used += w(s[i]); + if (used > maxW) + break; + } + return i; +} + +UInt16 TxtWrap(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w, UInt16 *consumed) +{ + UInt16 fit = TxtFit(s, len, maxW, w); + UInt16 brk, end; + + if (fit >= len) { + *consumed = len; + return len; + } + if (fit == 0) + fit = 1; /* always make progress, even if one character is too wide */ + /* break after the last space in the part that fits */ + for (brk = fit; brk > 0 && s[brk] != ' '; brk--) + ; + if (brk == 0) + brk = fit; /* one word longer than the line: break it */ + end = brk; + while (end > 0 && s[end - 1] == ' ') + end--; + while (brk < len && s[brk] == ' ') + brk++; + *consumed = brk; + return end; +} + +void TxtFormatAge(Char *out, UInt32 seconds) +{ + UInt32 v; + Char unit; + Char digits[12]; + UInt16 n = 0, i = 0; + + if (seconds < 60) { + out[0] = 'n'; out[1] = 'o'; out[2] = 'w'; out[3] = 0; + return; + } + if (seconds < 3600UL) { v = seconds / 60; unit = 'm'; } + else if (seconds < 86400UL) { v = seconds / 3600; unit = 'h'; } + else if (seconds < 31536000UL) { v = seconds / 86400; unit = 'd'; } + else { v = seconds / 31536000UL; unit = 'y'; } + do { + digits[n++] = (Char)('0' + v % 10); + v /= 10; + } while (v); + while (n) + out[i++] = digits[--n]; + out[i++] = unit; + out[i] = 0; +} + +void TxtFormatSnr(Char *out, Int8 quarterDb) +{ + Int16 q = quarterDb; + Int16 db = (q >= 0 ? q + 2 : q - 2) / 4; /* round half away from zero */ + UInt16 i = 0, n = 0; + Char digits[4]; + Int16 a = db < 0 ? -db : db; + + if (db > 0) + out[i++] = '+'; + else if (db < 0) + out[i++] = '-'; + do { + digits[n++] = (Char)('0' + a % 10); + a /= 10; + } while (a); + while (n) + out[i++] = digits[--n]; + out[i++] = 'd'; + out[i++] = 'B'; + out[i] = 0; +} + +UInt16 TxtSplitSender(const Char *text, UInt16 len) +{ + UInt16 i; + + for (i = 0; i < len && i <= VM_NAME_MAX; i++) { + if (text[i] == ':') + return (i > 0 && i + 1 < len && text[i + 1] == ' ') ? i : 0; + } + return 0; +} + +/* Windows-1252's 0x80-0x9F punctuation, for the Unicode characters people type. */ +static Char Cp1252(UInt32 cp) +{ + switch (cp) { + case 0x2026: return (Char)0x85; /* ellipsis */ + case 0x2018: return (Char)0x91; + case 0x2019: return (Char)0x92; + case 0x201C: return (Char)0x93; + case 0x201D: return (Char)0x94; + case 0x2022: return (Char)0x95; /* bullet */ + case 0x2013: return (Char)0x96; /* en dash */ + case 0x2014: return (Char)0x97; /* em dash */ + case 0x20AC: return (Char)0x80; /* euro */ + default: return '?'; + } +} + +UInt16 TxtUtf8ToPalm(const UInt8 *in, UInt16 inLen, Char *out, UInt16 outSize) +{ + UInt16 i = 0, o = 0; + + if (outSize == 0) + return 0; + while (i < inLen && o + 1 < outSize) { + UInt8 b = in[i]; + UInt32 cp; + UInt16 extra, k; + + if (b < 0x80) { + out[o++] = (Char)b; + i++; + continue; + } + if ((b & 0xE0) == 0xC0) { cp = b & 0x1F; extra = 1; } + else if ((b & 0xF0) == 0xE0) { cp = b & 0x0F; extra = 2; } + else if ((b & 0xF8) == 0xF0) { cp = b & 0x07; extra = 3; } + else { /* stray continuation or invalid byte */ + out[o++] = '?'; + i++; + continue; + } + for (k = 1; k <= extra; k++) { + if (i + k >= inLen || (in[i + k] & 0xC0) != 0x80) + break; + cp = (cp << 6) | (in[i + k] & 0x3F); + } + if (k <= extra) { /* truncated sequence */ + out[o++] = '?'; + i += k; + continue; + } + i += 1 + extra; + if (cp >= 0xA0 && cp <= 0xFF) + out[o++] = (Char)cp; + else + out[o++] = Cp1252(cp); + } + out[o] = 0; + return o; +} diff --git a/palm-client/src/ui/text.h b/palm-client/src/ui/text.h new file mode 100644 index 0000000..ba2fb6d --- /dev/null +++ b/palm-client/src/ui/text.h @@ -0,0 +1,49 @@ +/* + * Text layout and formatting for the small-font UI. Plain C (no Palm OS calls) + * so it is tested on the host with the real small-font width table. + */ +#ifndef VM_TEXT_H +#define VM_TEXT_H + +#include "../protocol.h" + +/* Width of one character in pixels (e.g. from gSmallFontWidths, or FntCharWidth on the device). */ +typedef Int16 (*TxtWidthFn)(Char c); + +/* The small font's widths from the generated table. */ +Int16 TxtSmallWidth(Char c); + +Int16 TxtWidth(const Char *s, UInt16 len, TxtWidthFn w); + +/* How many characters of s fit in maxW pixels. */ +UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w); + +/* + * One line of word wrap: returns how many characters to draw on this line + * (breaking after the last space that fits, or mid-word if a word is longer + * than the line) and sets *consumed to that plus any spaces to skip before + * the next line. Returns 0 with *consumed 0 only for empty input. + */ +UInt16 TxtWrap(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w, UInt16 *consumed); + +/* "now", "5m", "23h", "3d", "2y" (MeshTerm's aligned-column ages). */ +void TxtFormatAge(Char *out, UInt32 seconds); + +/* SNR in quarter dB as a whole number: "+6dB", "-3dB", "0dB". */ +void TxtFormatSnr(Char *out, Int8 quarterDb); + +/* + * MeshCore channel messages carry the sender in the text as "Name: message". + * Returns the name's length (0 if there's no such prefix); the message starts + * at name length + 2. + */ +UInt16 TxtSplitSender(const Char *text, UInt16 len); + +/* + * UTF-8 to the Visor's Latin-1 / Windows-1252 character set. Characters it + * can't show become '?'. Writes at most outSize - 1 bytes plus a terminator + * and returns the length. + */ +UInt16 TxtUtf8ToPalm(const UInt8 *in, UInt16 inLen, Char *out, UInt16 outSize); + +#endif diff --git a/palm-client/tests/test_text.c b/palm-client/tests/test_text.c new file mode 100644 index 0000000..8d64f71 --- /dev/null +++ b/palm-client/tests/test_text.c @@ -0,0 +1,123 @@ +/* Host-side tests for the small-font text layout. Run with `make test`. */ +#include +#include +#include "../src/ui/text.h" +#include "../src/ui/fontmetrics.h" + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \ +} while (0) + +/* Fixed-width stand-in: every character 4 px, so expectations are easy to read. */ +static Int16 Four(Char c) +{ + (void)c; + return 4; +} + +static void test_metrics(void) +{ + CHECK(TxtSmallWidth('A') == 4); + CHECK(TxtSmallWidth((Char)0x80) == 5); /* the star glyph is drawn 5 px wide */ + CHECK(TxtWidth("abc", 3, TxtSmallWidth) == 12); +} + +static void test_fit_and_wrap(void) +{ + const char *t = "I'll bring a spare antenna for anyone"; + UInt16 n, used; + + CHECK(TxtFit("abcdef", 6, 12, Four) == 3); + CHECK(TxtFit("abcdef", 6, 100, Four) == 6); + + /* 20 px = 5 characters: "I'll " fits, so the break is after "I'll" */ + n = TxtWrap(t, strlen(t), 20, Four, &used); + CHECK(n == 4 && used == 5); + + /* 13 chars wide: "I'll bring a " -> "I'll bring a" */ + n = TxtWrap(t, strlen(t), 13 * 4, Four, &used); + CHECK(n == 12 && memcmp(t, "I'll bring a", 12) == 0 && used == 13); + + /* a word longer than the line is broken */ + n = TxtWrap("https://example.com/long", 24, 10 * 4, Four, &used); + CHECK(n == 10 && used == 10); + + /* the rest fits */ + n = TxtWrap("short", 5, 100, Four, &used); + CHECK(n == 5 && used == 5); + + /* a whole transcript paragraph wraps into lines that each fit */ + { + const char *p = t; + UInt16 left = strlen(t), lines = 0; + + while (left) { + n = TxtWrap(p, left, 26 * 4, TxtSmallWidth, &used); + CHECK(TxtWidth(p, n, TxtSmallWidth) <= 26 * 4); + CHECK(used > 0); + p += used; + left -= used; + lines++; + } + CHECK(lines == 2); + } +} + +static void test_age_and_snr(void) +{ + char buf[16]; + + TxtFormatAge(buf, 0); CHECK(strcmp(buf, "now") == 0); + TxtFormatAge(buf, 59); CHECK(strcmp(buf, "now") == 0); + TxtFormatAge(buf, 300); CHECK(strcmp(buf, "5m") == 0); + TxtFormatAge(buf, 82800); CHECK(strcmp(buf, "23h") == 0); + TxtFormatAge(buf, 259200); CHECK(strcmp(buf, "3d") == 0); + TxtFormatAge(buf, 63072000UL); CHECK(strcmp(buf, "2y") == 0); + + TxtFormatSnr(buf, 24); CHECK(strcmp(buf, "+6dB") == 0); + TxtFormatSnr(buf, 26); CHECK(strcmp(buf, "+7dB") == 0); /* 6.5 rounds away from zero */ + TxtFormatSnr(buf, -10); CHECK(strcmp(buf, "-3dB") == 0); /* -2.5 */ + TxtFormatSnr(buf, 1); CHECK(strcmp(buf, "0dB") == 0); + TxtFormatSnr(buf, 127); CHECK(strcmp(buf, "+32dB") == 0); +} + +static void test_sender_split(void) +{ + CHECK(TxtSplitSender("Alice: hi all", 13) == 5); + CHECK(TxtSplitSender("no sender here", 14) == 0); + CHECK(TxtSplitSender("time 12:30 ok", 13) == 0); /* colon not followed by a space */ + CHECK(TxtSplitSender(": empty name", 12) == 0); + CHECK(TxtSplitSender("Alice:", 6) == 0); +} + +static void test_utf8(void) +{ + char out[32]; + static const UInt8 cafe[] = { 'c', 'a', 'f', 0xC3, 0xA9 }; + static const UInt8 dots[] = { 'a', 0xE2, 0x80, 0xA6 }; + static const UInt8 emoji[] = { 'h', 'i', ' ', 0xF0, 0x9F, 0x91, 0x8B }; + static const UInt8 broken[] = { 'x', 0xC3 }; + + CHECK(TxtUtf8ToPalm(cafe, sizeof(cafe), out, sizeof(out)) == 4 && (unsigned char)out[3] == 0xE9); + CHECK(TxtUtf8ToPalm(dots, sizeof(dots), out, sizeof(out)) == 2 && (unsigned char)out[1] == 0x85); + CHECK(TxtUtf8ToPalm(emoji, sizeof(emoji), out, sizeof(out)) == 4 && strcmp(out, "hi ?") == 0); + CHECK(TxtUtf8ToPalm(broken, sizeof(broken), out, sizeof(out)) == 2 && strcmp(out, "x?") == 0); + CHECK(TxtUtf8ToPalm((const UInt8 *)"abcdef", 6, out, 4) == 3 && strcmp(out, "abc") == 0); +} + +int main(void) +{ + test_metrics(); + test_fit_and_wrap(); + test_age_and_snr(); + test_sender_split(); + test_utf8(); + if (failures) { + printf("%d failure(s)\n", failures); + return 1; + } + printf("all text tests passed\n"); + return 0; +}