From 24db6fc483d61625d589b732efce3a681db45762 Mon Sep 17 00:00:00 2001 From: Neo <321592+Neo-Desktop@users.noreply.github.com> Date: Tue, 10 Nov 2026 18:16:53 -0800 Subject: [PATCH] BBS terminals: RIPscrip, Avatar, Amiga ANSI, Atari ST (Phase 7) (#43) * Atari ST terminals, and pure white kept as a colour The Atari ST's TOS VT52 in medium resolution (80x25, four colours) and low (40x25, sixteen): ESC b and ESC c pens, reverse, wrap, the cursor saved and hidden, the extra erases, line insert and delete, and the ST's character set. Vt52Interpreter gains a character hook and an argument hook for it; the machine size and palette set-up is shared with the byte-oriented machines (useMachine, releaseMachine). colorOverride's "none" was -1, which is also opaque white, so a machine's pure white (the ST's screen, Viewdata's and the Minitel's white) fell back to the theme's colour. "None" is now NO_COLOR (transparent black), and overrides are kept opaque. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY * Avatar (AVT/0 and AVT/0+) over ANSI-BBS FidoNet's compact codes taken in ANSI-BBS's intercept hook: ^L, ^Y's repeat, ^V's PC attributes (DLE-quoted too), blink, cursor moves and place, clear to the line's end, and AVT/0+'s insert mode, area scrolls, clears and fills (through Rectangles), character delete and pattern repeat. ANSI, music and the rest of ANSI-BBS work underneath. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY * Amiga ANSI ANSI-BBS in Latin-1, drawn 8x8 (Unscii's glyphs, Topaz's size; the generator now adds Latin-1's to the 8x8 set) in colours from the Amiga's 12-bit palette. An ANSI-BBS profile can now carry its own sixteen colours (AnsiBbsProfile.colors), which its interpreter puts over the theme's and takes back off when another profile takes over. The per-profile render test now uses the profile's character set, as connecting does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY * RIPscrip 1.54 ANSI-BBS whose lines starting !| (or ^A|, ^B|) are RIP's commands on a 640x350 EGA screen over an 80x43 text window: lines in their styles and thickness, rectangles, bars, circles, ellipses, arcs, pies, Beziers, polygons and polylines, flood fills, BGI's fill patterns and the host's, text, the palette, the viewport and text window, the clipboard, text blocks, and mouse regions and buttons. ESC [ ! answers RIP's version, and ESC [ 1 ! and 2 ! turn it off and on. Line continuation and the CR LF ending a RIP line are the line's own. Raster gains ellipses, patterned fills, flood fill, a clip rectangle and grab and put, and draws shrunk glyphs with their rows merged (a thin stroke was lost). Taps: an interpreter can take a tap at a point on the screen (TerminalEmulator.tap); Android's view and the iOS screen (through TerminalBridge.tap) send what it gives, so RIP's buttons work. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSyCmXjaURxSYoHxCY --------- Co-authored-by: Claude --- .../app/android/terminal/TerminalView.kt | 10 +- .../app/android/terminal/ProfileRenderTest.kt | 6 + docs/features.md | 25 +- docs/terminals.md | 10 + ios/DModem/Sources/TerminalModel.swift | 5 + ios/DModem/Sources/TerminalScreen.swift | 6 +- .../dmodem/app/terminal/AnsiBbsInterpreter.kt | 8 +- .../kotlin/org/dmodem/app/terminal/AtariSt.kt | 144 +++++ .../kotlin/org/dmodem/app/terminal/Avatar.kt | 171 ++++++ .../app/terminal/ByteControlInterpreter.kt | 30 +- .../org/dmodem/app/terminal/LegacyCharsets.kt | 73 ++- .../kotlin/org/dmodem/app/terminal/Raster.kt | 139 ++++- .../kotlin/org/dmodem/app/terminal/Rip.kt | 567 ++++++++++++++++++ .../org/dmodem/app/terminal/TerminalBridge.kt | 3 + .../dmodem/app/terminal/TerminalEmulator.kt | 24 +- .../app/terminal/TerminalInterpreter.kt | 3 + .../dmodem/app/terminal/TerminalProfile.kt | 38 +- .../org/dmodem/app/terminal/TerminalRuns.kt | 2 +- .../dmodem/app/terminal/Vt420Interpreter.kt | 2 +- .../dmodem/app/terminal/Vt52Interpreter.kt | 22 +- .../org/dmodem/app/terminal/VtInterpreter.kt | 2 +- .../org/dmodem/app/terminal/AmigaAnsiTest.kt | 23 + .../org/dmodem/app/terminal/AtariStTest.kt | 108 ++++ .../org/dmodem/app/terminal/AvatarTest.kt | 70 +++ .../org/dmodem/app/terminal/PetsciiTest.kt | 2 +- .../kotlin/org/dmodem/app/terminal/RipTest.kt | 108 ++++ .../dmodem/app/terminal/XtermFeaturesTest.kt | 4 +- tools/gen_legacy_charsets.py | 4 +- 28 files changed, 1535 insertions(+), 74 deletions(-) create mode 100644 shared/src/commonMain/kotlin/org/dmodem/app/terminal/AtariSt.kt create mode 100644 shared/src/commonMain/kotlin/org/dmodem/app/terminal/Avatar.kt create mode 100644 shared/src/commonMain/kotlin/org/dmodem/app/terminal/Rip.kt create mode 100644 shared/src/commonTest/kotlin/org/dmodem/app/terminal/AmigaAnsiTest.kt create mode 100644 shared/src/commonTest/kotlin/org/dmodem/app/terminal/AtariStTest.kt create mode 100644 shared/src/commonTest/kotlin/org/dmodem/app/terminal/AvatarTest.kt create mode 100644 shared/src/commonTest/kotlin/org/dmodem/app/terminal/RipTest.kt diff --git a/app/src/main/java/org/dmodem/app/android/terminal/TerminalView.kt b/app/src/main/java/org/dmodem/app/android/terminal/TerminalView.kt index 171bf70..741c292 100644 --- a/app/src/main/java/org/dmodem/app/android/terminal/TerminalView.kt +++ b/app/src/main/java/org/dmodem/app/android/terminal/TerminalView.kt @@ -310,7 +310,7 @@ class TerminalView @JvmOverloads constructor( } if (i >= 0) { val o = e.colorOverride(i) - if (o != -1) return o + if (o != TerminalEmulator.NO_COLOR) return o } } return theme.argb(c, isFg) @@ -589,7 +589,7 @@ class TerminalView @JvmOverloads constructor( val x = px(col) val cw = px(col + 1) - x val y = e.cursorY * cellH - val cursorColor = e.colorOverride(258).takeIf { it != -1 } ?: theme.cursorArgb + val cursorColor = e.colorOverride(258).takeIf { it != TerminalEmulator.NO_COLOR } ?: theme.cursorArgb fillPaint.color = cursorColor // the remote's shape (DECSCUSR), else the user's val shape = when (e.cursorStyle) { @@ -689,6 +689,12 @@ class TerminalView @JvmOverloads constructor( val (row, col) = cellAt(x, y) if (row < 0) return false val e = s.emulator + // (a terminal that takes taps itself: a RIP button's text) + val taken = synchronized(e) { e.tap(x / (cellW * e.cols).toDouble(), y / (cellH * e.rows).toDouble()) } + if (taken != null) { + s.sendTerminal(taken) + return true + } val (down, up) = synchronized(e) { e.mouseEvent(0, col, row, press = true) to e.mouseEvent(0, col, row, press = false) } diff --git a/app/src/test/java/org/dmodem/app/android/terminal/ProfileRenderTest.kt b/app/src/test/java/org/dmodem/app/android/terminal/ProfileRenderTest.kt index 1f52409..d18386a 100644 --- a/app/src/test/java/org/dmodem/app/android/terminal/ProfileRenderTest.kt +++ b/app/src/test/java/org/dmodem/app/android/terminal/ProfileRenderTest.kt @@ -40,6 +40,8 @@ class ProfileRenderTest { v.session = session v.applySettings(TerminalSettings(cursorBlink = false)) session.profile = profile + // (as connecting does: the profile's character set, unless the entry has its own) + profile.charset?.let { session.charset = it } v.measure(View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY)) v.layout(0, 0, w, h) val b = ByteArray(octets.length) { octets[it].code.toByte() } @@ -66,6 +68,10 @@ class ProfileRenderTest { "tek4014" -> "\u001d${tek(100, 100)}${tek(900, 100)}${tek(500, 700)}${tek(100, 100)}\u001fTEKTRONIX" "vt340" -> "Hello VT340 $e[7mInverse$e[0m\r\n" + "${e}PpS(M2(H120L50S100))W(I2)P[400,240]C[+100]P[100,300]F(V[+100][+0,+100][-100])$e\\" + "atari-st", "atari-st-low" -> "${e}EHello ST ${e}b1Red${e}b\u000f ${e}pInverse${e}q \u009e\u00e0" + "avatar" -> "\u000c\u0016\u0001\u001eAvatar \u0019*\u0008\u0016\u0001\u0004 red \u0016\u0001\u0007 $e[1;32mANSI too$e[0m" + "amiga" -> "$e[2J$e[HAmiga $e[1;31mANSI$e[0m in Latin-1: \u00e9\u00e8\u00fc\u00df\u00a9 $e[44;33m Topaz $e[0m" + "rip" -> "!|*|S0101|B0000HR0K|c0F|@0806RIPscrip on D-Modem|c0E|C8W4628|S0904|o46461O14|c0A|L0078HR78|1UC83CFK4G0000<>Enter<>^M\r\nANSI text in the window" "vt52" -> "${e}H${e}JHello VT52 ${e}F" + "abcdefg" + "${e}G" else -> "$e[2J$e[HHello ${p.label} $e[1mBold$e[0m $e[7mInverse$e[0m $e[4mUnder$e[0m\r\n" + "$e(0lqqqqk$e(B $e[31mRed$e[0m" diff --git a/docs/features.md b/docs/features.md index 209a0de..78f3c2d 100644 --- a/docs/features.md +++ b/docs/features.md @@ -30,9 +30,9 @@ clear-home, bold as bright, iCE colours, and box drawing and block characters drawn to fill their cells so ANSI art has no gaps. A terminal type (Settings, and each directory entry's own) picks what it - is: xterm, ANSI-BBS (a PC's, in CP437, as SyncTERM is), DEC VT52, VT100, + is: xterm, ANSI-BBS (a PC's, in CP437, as SyncTERM is), RIPscrip, Avatar, Amiga ANSI, DEC VT52, VT100, VT102, VT220, VT320, VT340, VT420, VT510, VT520 or VT525, a Commodore, an - Atari, Viewdata (Prestel), a Minitel or a Tektronix 4014. Each + Atari 8-bit or ST, Viewdata (Prestel), a Minitel or a Tektronix 4014. Each answers the device attribute requests as that terminal did, knows only its own sequences (a VT100 has no colours, a VT220 no xterm titles or mouse), and gives SSH its `TERM` and Telnet its TERMINAL-TYPE. The DEC @@ -79,7 +79,19 @@ `ESC [ M` when a PLAY string follows it) played as a PC speaker would, iCE colours when the BBS asks (`ESC [ ? 33 h`), the line speed it asks to be shown at for ANSI animations (`ESC [ 0 ; n * r`), and doorway - mode for DOS doors (`ESC [ = 255 h`: the keys as a PC's scan codes). The + mode for DOS doors (`ESC [ = 255 h`: the keys as a PC's scan codes). + Avatar is ANSI-BBS with FidoNet's compact codes as well (AVT/0 and + AVT/0+: ^L, ^Y's repeat, ^V's PC attributes, cursor moves, insert mode, + area scrolls, clears and fills, and pattern repeats). RIPscrip (1.54) + is ANSI-BBS whose lines starting `!|` draw on a 640x350 EGA screen over + an 80x43 text window: lines in their styles and thicknesses, + rectangles, bars, circles, ellipses, arcs, pie slices, Béziers, + polygons, flood fills in BGI's patterns or the host's, text, the + palette, viewport and text window, the clipboard, and mouse regions + and buttons that send their text when tapped; it answers `ESC [ !` + with its version. Amiga ANSI is + ANSI-BBS in Latin-1, the Amiga's set, drawn 8x8 as Topaz is (Unscii's + glyphs) in colours from the Amiga's palette. The Commodore types (C64, C128 in 40 or 80 columns, Plus/4, VIC-20, PET in 40 or 80 columns) speak PETSCII as CCGMS and Novaterm do: the screen becomes the machine's (40x25, the VIC-20's 22x23) in its palette, drawn @@ -95,7 +107,12 @@ inverse video, its cursor keys wrapping round the screen, CLEAR, EOL, insert and delete of characters and lines, tabs, the buzzer and ESC to show a control's character; CTRL with a letter types its graphic and - the key bar's INV key types the next character inverse. Viewdata is + the key bar's INV key types the next character inverse. The Atari ST + types (80 columns in medium resolution's four colours, or 40 in low + resolution's sixteen) are TOS's VT52 with its additions: the pens (ESC + b, ESC c), reverse, wrap, the cursor saved and hidden, erases to the + screen's and line's start, line insert and delete, and the ST's own + character set. Viewdata is Prestel's 40x24 of teletext: its attributes take a cell and change the rest of the row (alpha and mosaic colours, flash, conceal, contiguous or separated mosaics, hold, new and black background; double height is diff --git a/docs/terminals.md b/docs/terminals.md index d6198f1..2ffdc62 100644 --- a/docs/terminals.md +++ b/docs/terminals.md @@ -18,6 +18,9 @@ graphics terminals draw through `Raster.kt`. |---|---|---|---|---| | xterm | `xterm` | `xterm-256color` / `XTERM-256COLOR` | any | The default: a VT220 with 256 and 24-bit colour, sixel, the mouse, titles, and Tek mode | | ANSI-BBS (PC) | `ansi-bbs` | `ansi` / `ANSI` | 80x25 | A PC's BBS terminal, as SyncTERM is: CP437 in a VGA font, iCE colours, ANSI music, doorway mode | +| RIPscrip | `rip` | `rip` / `ANSI` | 80x43 text over 640x350 graphics | RIPscrip 1.54 over ANSI-BBS: `!|` lines draw (lines, shapes, fills, text, the clipboard), mouse regions and buttons send their text when tapped, `ESC [ !` answers RIP's version | +| Avatar (AVT/0+) | `avatar` | `avatar` / `ANSI` | 80x25 | ANSI-BBS with FidoNet's Avatar codes: ^L, ^Y repeat, ^V's attributes, moves and AVT/0+'s area scrolls, clears, fills and pattern repeat | +| Amiga ANSI | `amiga` | `amiga` / `ANSI` | 80x25 | ANSI-BBS in Latin-1 with 8x8 glyphs (Topaz's size, Unscii's drawing) and colours from the Amiga's 12-bit palette | | DEC VT52 | `vt52` | `vt52` / `DEC-VT52` | 80x24 | The VT52's own escape codes and graphics | | DEC VT100 | `vt100` | `vt100` / `DEC-VT100` | 80/132x24 | ANSI codes at level 1: no colour; double-size lines; VT52 mode | | DEC VT102 | `vt102` | `vt102` / `DEC-VT102` | 80/132x24 | Adds character and line insert and delete, and a printer port | @@ -29,6 +32,7 @@ graphics terminals draw through `Raster.kt`. | DEC VT525 | `vt525` | `vt525` / `DEC-VT525` | 80/132x24+ | A VT520 in colour, with its colour table | | Commodore 64, 128 (40 and 80), Plus/4, VIC-20, PET (40 and 80) | `c64`, `c128`, `c128-80`, `plus4`, `vic20`, `pet`, `pet80` | `petscii` / `PETSCII` | 40x25 (VIC-20: 22x23; 80 for the C128's VDC and the 8032) | PETSCII as CCGMS and Novaterm do it, each in its machine's palette | | Atari 8-bit | `atari`, `atari-intl` | `atascii` / `ATASCII` | 40x24 | ATASCII and the screen editor's controls; the XL's international set as a second type | +| Atari ST | `atari-st`, `atari-st-low` | `st52` / `VT52` | 80x25 in 4 colours, or 40x25 in 16 | TOS's VT52 with its colours (ESC b, ESC c), reverse, wrap, cursor save and on/off, more erases, line insert and delete, in the ST's character set | | Viewdata (Prestel) | `viewdata` | `viewdata` / `VIEWDATA` | 40x24 | Teletext Level 1 attributes and mosaics; # sends | | Minitel | `minitel` | `minitel` / `MINITEL` | 40x25 | Teletel videotex with the status row, the protocol commands, Mixte (80 columns) and Téléinformatique (a VT100) | | Tektronix 4014 | `tek4014` | `tek4014` / `TEK4014` | 4096x3120 tube | Vectors, points, incremental plots and alpha text on a storage tube | @@ -104,6 +108,12 @@ table when they are. - A colour register changed after drawing doesn't recolour what's already drawn. +**RIPscrip** +- Icons (1I, 1W) and scenes (1R) from the host's files aren't loaded. +- Text variables ($…$) aren't expanded. +- The stroke fonts are drawn with the PC's bitmap font at their size. +- Vertical text runs down, but its glyphs don't turn. + **Tektronix** - No graphic input (GIN) cross-hair. - No write-through or defocused modes. diff --git a/ios/DModem/Sources/TerminalModel.swift b/ios/DModem/Sources/TerminalModel.swift index e761c81..d9a59d9 100644 --- a/ios/DModem/Sources/TerminalModel.swift +++ b/ios/DModem/Sources/TerminalModel.swift @@ -103,6 +103,11 @@ final class TerminalModel: ObservableObject { revision = bridge.revision } + /// a tap at ([fx], [fy]), fractions of the screen from its top left: what the terminal sends for it (a RIP button's text), if anything + func tap(fx: CGFloat, fy: CGFloat) { + out(bridge.tap(fx: Double(fx), fy: Double(fy))) + } + /// text typed (the keyboard's), Ctrl and Alt (a Commodore's C=) if the sticky ones are down func typed(_ text: String) { out(bridge.typed(text: text, ctrl: ctrl, alt: alt)) diff --git a/ios/DModem/Sources/TerminalScreen.swift b/ios/DModem/Sources/TerminalScreen.swift index a0497f0..c0b0dde 100644 --- a/ios/DModem/Sources/TerminalScreen.swift +++ b/ios/DModem/Sources/TerminalScreen.swift @@ -28,7 +28,11 @@ struct TerminalScreen: View { .onChange(of: model.bridge.glyphHeight) { _ in fit(geo.size, metrics(geo.size)) } .overlay(KeyInput(model: model, focused: $focused).frame(width: 1, height: 1).opacity(0.01), alignment: .topLeading) .contentShape(Rectangle()) - .onTapGesture { focused = true } + .onTapGesture { p in + focused = true + // (a terminal that takes taps itself: a RIP button's text to the modem) + model.tap(fx: p.x / (cell.width * CGFloat(model.cols)), fy: p.y / (cell.height * CGFloat(model.rows))) + } .gesture(MagnificationGesture().onEnded { s in fontSize = min(24, max(7, fontSize * Double(s))) }) } // a VT320's status line, below the screen (the screen a line shorter) diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AnsiBbsInterpreter.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AnsiBbsInterpreter.kt index f55368e..704865e 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AnsiBbsInterpreter.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AnsiBbsInterpreter.kt @@ -10,7 +10,7 @@ package org.dmodem.app.terminal * 255 h: the keys as a PC's scan codes), and font selection (CSI SP D: * only code page 437 is there, so the others are taken and ignored). */ -open class AnsiBbsInterpreter(t: TerminalEmulator, profile: AnsiBbsProfile) : VtInterpreter(t, profile) { +open class AnsiBbsInterpreter(t: TerminalEmulator, private val bbs: AnsiBbsProfile) : VtInterpreter(t, bbs) { /** which CSIs start ANSI music (CSI = n M): 0 only CSI |, 1 CSI N too, 2 CSI M too (unless it has parameters: then it's DL) */ private var musicMode = 2 /** the music being gathered, until SO; null outside it */ @@ -25,16 +25,18 @@ open class AnsiBbsInterpreter(t: TerminalEmulator, profile: AnsiBbsProfile) : Vt attach() } - /** the PC's 80x25 */ + /** the PC's 80x25, and the profile's own sixteen colours if it has them */ private fun attach() { t.requestedColumns = 80 t.requestedRows = 25 + bbs.colors?.forEachIndexed { i, c -> t.setColorOverride(i, c) } } - /** the view's size back, for the profile taking over */ + /** the view's size (and the theme's colours) back, for the profile taking over */ override fun detach() { t.requestedColumns = 0 t.requestedRows = 0 + if (bbs.colors != null) t.resetColors(0..15) t.iceColors = false t.emulatedBps = 0 } diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AtariSt.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AtariSt.kt new file mode 100644 index 0000000..adacc6e --- /dev/null +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/AtariSt.kt @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +/** + * An Atari ST's terminal: TOS's VT52 with its own additions (colours, + * the cursor on and off and saved, more erases, reverse, wrap, line + * insert and delete) in the ST's character set. This one is medium + * resolution's 80x25 in four colours (white, red, green, black: the + * text black on white, as the desktop is); [AtariStLowProfile] is low + * resolution's 40x25 in sixteen. + */ +open class AtariStProfile : TerminalProfile(), ByteMachine { + override val id get() = "atari-st" + override val label get() = "Atari ST (80 col.)" + override val term get() = "st52" + // (BBSes know it as the VT52 it is) + override val telnetType get() = "VT52" + + override val columns get() = 80 + override val rows get() = 25 + override val palette: IntArray get() = TOS_PALETTE.copyOfRange(0, 3) + TOS_PALETTE[15] + /** the highest pen on the first (white) */ + override val defaultColor get() = palette.size - 1 + override val background get() = 0 + + /** the standard keys and the keypad */ + override val keyBar: List get() = KeyBars.DEC + + /** an [AtariStInterpreter] */ + override fun interpreter(t: TerminalEmulator): TerminalInterpreter = AtariStInterpreter(t, this) + + companion object { + /** TOS's sixteen colours at power-up (its 3-bit levels): white, red, green, yellow, blue, magenta, cyan, light grey, grey, light red, light green, light yellow, light blue, light magenta, light cyan, black */ + val TOS_PALETTE: IntArray = intArrayOf( + 0x777, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0x555, + 0x333, 0x733, 0x373, 0x773, 0x337, 0x737, 0x377, 0x000, + ).map { c -> + /** a 3-bit level as 0-255 */ + fun level(v: Int) = v * 255 / 7 + (0xff shl 24) or (level(c shr 8 and 7) shl 16) or (level(c shr 4 and 7) shl 8) or level(c and 7) + }.toIntArray() + } +} + +/** An Atari ST in low resolution: 40x25 in TOS's sixteen colours */ +open class AtariStLowProfile : AtariStProfile() { + override val id get() = "atari-st-low" + override val label get() = "Atari ST (40 col., 16 colours)" + override val columns get() = 40 + override val palette: IntArray get() = TOS_PALETTE +} + +/** TOS's VT52: the VT52's sequences and the ST's own, the octets in the ST's set */ +open class AtariStInterpreter(t: TerminalEmulator, private val m: AtariStProfile) : + Vt52Interpreter(t, GSet.VT52_GRAPHICS, "\u001b/K") { + private var fg = m.defaultColor + private var bg = m.background + private var reverse = false + + init { + attach() + } + + override val readsOctets: Boolean get() = true + + /** the ST's screen and colours on the emulator, wrap on, the cursor shown */ + private fun attach() { + t.useMachine(m) + t.autowrap = true + t.cursorVisible = true + restyle() + } + + /** what's written from now on: the pens, reversed or not */ + private fun restyle() { + t.style = Style.of(fg, bg, if (reverse) Style.INVERSE else 0) + } + + /** an octet in the ST's set */ + override fun charOf(cp: Int): Int = if (cp in 0x80..0xff) HIGH[cp - 0x80].code else cp + + /** the ST's own after ESC: E clear, d erase above, e and f the cursor, j and k its place saved, l and o line erases, p and q reverse, v and w wrap, L and M lines, b and c the colours */ + override fun esc(cp: Int) { + when (cp.toChar()) { + 'E' -> { + t.eraseDisplay(2) + t.moveCursor(0, 0) + } + 'd' -> t.eraseDisplay(1) + 'e' -> t.cursorVisible = true + 'f' -> t.cursorVisible = false + 'j' -> t.saveCursor() + 'k' -> t.restoreCursor() + 'l' -> { + t.eraseLine(2) + t.carriageReturn() + } + 'o' -> t.eraseLine(1) + 'p' -> { reverse = true; restyle() } + 'q' -> { reverse = false; restyle() } + 'v' -> t.autowrap = true + 'w' -> t.autowrap = false + 'L' -> { + t.insertLines(1) + t.carriageReturn() + } + 'M' -> { + t.deleteLines(1) + t.carriageReturn() + } + 'b', 'c' -> takeArgument(cp) + else -> super.esc(cp) + } + } + + /** ESC b's and ESC c's pen: its low bits (as many as the resolution has colours) */ + override fun escArgument(command: Int, arg: Int) { + val pen = arg and (m.palette.size - 1) + if (command == 'b'.code) fg = pen else bg = pen + restyle() + } + + /** RIS: the pens and reverse as at the start, the screen the ST's */ + override fun reset() { + super.reset() + fg = m.defaultColor + bg = m.background + reverse = false + attach() + } + + /** the view's size and the theme's colours back */ + override fun detach() { + t.releaseMachine() + } + + companion object { + /** the ST's set from 0x80: Latin letters, Hebrew, Greek and mathematics */ + private const val HIGH = + "ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥ßƒáíóúñѪº¿⌐¬½¼¡«»ãõØøœŒÀÃÕ¨´†¶©®™" + + "ijIJאבגדהוזחטיכלמנסעפצקרשתןךםףץ§∧∞αβΓπΣσµτΦΘΩδ∮ϕ∈∩≡±≥≤⌠⌡÷≈°•·√ⁿ²³¯" + } +} diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Avatar.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Avatar.kt new file mode 100644 index 0000000..c8fc985 --- /dev/null +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Avatar.kt @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +/** + * Avatar, FidoNet's compact codes (AVT/0, FSC-0025, and AVT/0+, FSC-0037) + * over ANSI-BBS: what a BBS set to Avatar sends, and the ANSI it mixes in. + */ +open class AvatarProfile : AnsiBbsProfile() { + override val id get() = "avatar" + override val label get() = "Avatar (AVT/0+)" + override val term get() = "avatar" + // (BBSes find ANSI by it; Avatar is chosen on the BBS) + override val telnetType get() = "ANSI" + + /** an [AvatarInterpreter] */ + override fun interpreter(t: TerminalEmulator): TerminalInterpreter = AvatarInterpreter(t, this) +} + +/** + * Avatar's codes taken before ANSI-BBS's parser sees them: ^L (clear, + * cyan), ^Y (repeat a character), and ^V's commands: a PC attribute + * byte, blink, the cursor's moves and place, clear to the line's end, and + * AVT/0+'s insert mode, area scrolls, clears and fills, character delete + * and pattern repeat. + */ +open class AvatarInterpreter(t: TerminalEmulator, profile: AvatarProfile) : AnsiBbsInterpreter(t, profile) { + private val rectangles = Rectangles(t) + /** the command whose argument bytes are being gathered (^Y: [REPEAT], ^V's: its code), -1 none; [LEAD]: ^V's code to come */ + private var command = NONE + private val args = ArrayList() + /** the arguments the command takes (^V ^Y's grows once its length comes) */ + private var need = 0 + /** ^V ^A's DLE: the next byte the attribute, whatever it is */ + private var quoted = false + + /** an Avatar code, or one of its arguments: taken; else ANSI-BBS's (its music first) */ + override fun intercept(cp: Int): Boolean { + if (command != NONE) { + argument(octet(cp)) + return true + } + if (super.intercept(cp)) return true + when (cp) { + 0x0c -> { + t.insertMode = false + attribute(3) + t.eraseDisplay(2) + t.moveCursor(0, 0) + } + 0x19 -> expect(REPEAT, 2) + 0x16 -> command = LEAD + else -> return false + } + return true + } + + /** a code point as the octet it came as (the CP437 the profile reads) */ + private fun octet(cp: Int): Int = Cp437.encode(cp) + + /** command [c]'s [n] arguments to gather */ + private fun expect(c: Int, n: Int) { + command = c + need = n + args.clear() + quoted = false + if (n == 0) run() + } + + /** one byte after ^V or ^Y: a command's code, or one of its arguments (its last running it) */ + private fun argument(b: Int) { + if (command == LEAD) { + // (any ^V command but insert mode's own ends insert mode) + if (b != 0x09) t.insertMode = false + expect(b, ARGS[b] ?: 0) + return + } + if (command == 0x01 && b == 0x10 && !quoted) { + quoted = true + return + } + args.add(b) + // ^V ^Y: its pattern's length, the pattern, then the count + if (command == 0x19 && args.size == 1) need = 1 + b + 1 + if (args.size >= need) run() + } + + /** the gathered command carried out */ + private fun run() { + val c = command + command = NONE + val a = args + when (c) { + REPEAT -> repeat(Cp437.decode(a[0]), a[1]) + 0x01 -> attribute(a[0]) + 0x02 -> t.style = Style.withAttrs(t.style, Style.attrs(t.style) or Style.BLINK) + 0x03 -> t.moveCursor(t.cursorX, t.cursorY - 1) + 0x04 -> t.moveCursor(t.cursorX, t.cursorY + 1) + 0x05 -> t.moveCursor(t.cursorX - 1, t.cursorY) + 0x06 -> t.moveCursor(t.cursorX + 1, t.cursorY) + 0x07 -> t.eraseLine(0) + 0x08 -> t.moveCursor(a[1] - 1, a[0] - 1) + 0x09 -> t.insertMode = true + 0x0a, 0x0b -> scroll(up = c == 0x0a, n = a[0], top = a[1], left = a[2], bottom = a[3], right = a[4]) + 0x0c -> area(a[0], ' '.code, a[1], a[2]) + 0x0d -> area(a[0], Cp437.decode(a[1]), a[2], a[3]) + 0x0e -> t.deleteChars(1) + 0x19 -> { + val n = a[0] + val pattern = a.subList(1, 1 + n).map { Cp437.decode(it) } + repeat(a[1 + n]) { pattern.forEach(t::print) } + } + } + } + + /** character [cp] printed [n] times */ + private fun repeat(cp: Int, n: Int) { + repeat(n) { t.print(cp) } + } + + /** a PC attribute byte as the style: its colours (CGA order to ANSI's), bit 7 blink */ + private fun attribute(a: Int) { + val fg = PC_TO_ANSI[a and 7] + (a and 8) + val bg = PC_TO_ANSI[(a shr 4) and 7] + t.style = Style.of(fg, bg, if (a and 0x80 != 0) Style.BLINK else 0) + } + + /** AVT/0+'s scroll of the area ([top], [left], [bottom], [right], from 1) [n] lines up or down (0: the area blanked), the lines it leaves blank */ + private fun scroll(up: Boolean, n: Int, top: Int, left: Int, bottom: Int, right: Int) { + val y0 = (top - 1).coerceIn(0, t.rows - 1) + val y1 = (bottom - 1).coerceIn(0, t.rows - 1) + val x0 = (left - 1).coerceIn(0, t.cols - 1) + val x1 = (right - 1).coerceIn(0, t.cols - 1) + if (y0 > y1 || x0 > x1) return + val lines = y1 - y0 + 1 + val k = if (n == 0) lines else n.coerceAtMost(lines) + if (k < lines) { + if (up) rectangles.copy(Rectangles.Area(y0 + k, x0, y1, x1), y0, x0) + else rectangles.copy(Rectangles.Area(y0, x0, y1 - k, x1), y0 + k, x0) + } + val blank = if (up) Rectangles.Area(y1 - k + 1, x0, y1, x1) else Rectangles.Area(y0, x0, y0 + k - 1, x1) + rectangles.erase(blank, selective = false) + } + + /** AVT/0+'s area at the cursor, [rows] by [cols] (each counted on from the cursor's), filled with [cp] in attribute [attr], which stays */ + private fun area(attr: Int, cp: Int, rows: Int, cols: Int) { + attribute(attr) + val a = Rectangles.Area( + t.cursorY, t.cursorX, + (t.cursorY + rows).coerceAtMost(t.rows - 1), (t.cursorX + cols).coerceAtMost(t.cols - 1), + ) + if (cp == ' '.code) rectangles.erase(a, selective = false) else rectangles.fill(a, cp) + } + + /** RIS: no Avatar code under way */ + override fun reset() { + super.reset() + command = NONE + args.clear() + } + + companion object { + private const val NONE = -1 + private const val LEAD = -2 + private const val REPEAT = -3 + /** each ^V command's argument count (^V ^Y's first: its length) */ + private val ARGS = mapOf(0x01 to 1, 0x08 to 2, 0x0a to 5, 0x0b to 5, 0x0c to 3, 0x0d to 4, 0x19 to 1) + /** a CGA colour number (blue first) as ANSI's (red first) */ + private val PC_TO_ANSI = intArrayOf(0, 4, 2, 6, 1, 5, 3, 7) + } +} diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/ByteControlInterpreter.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/ByteControlInterpreter.kt index cd36490..e356caf 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/ByteControlInterpreter.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/ByteControlInterpreter.kt @@ -29,22 +29,14 @@ abstract class ByteControlInterpreter(t: TerminalEmulator, private val machine: /** the machine's size and palette on the emulator */ private fun attach() { - t.requestedColumns = machine.columns - t.requestedRows = machine.rows + t.useMachine(machine) t.autowrap = true - val p = machine.palette - for (i in p.indices) t.setColorOverride(i, p[i]) - t.setColorOverride(256, p[machine.defaultColor]) - t.setColorOverride(257, p[machine.background]) t.style = look() } /** the view's size and the theme's colours back, for the profile taking over */ override fun detach() { - t.requestedColumns = 0 - t.requestedRows = 0 - t.resetColors(0..257) - t.style = Style.NORMAL + t.releaseMachine() } /** the machine as it starts: its colour, no reverse, the tab stops, its screen and palette */ @@ -198,6 +190,24 @@ abstract class ByteControlInterpreter(t: TerminalEmulator, private val machine: } } +/** [m]'s screen size and palette (its default colours the screen's own) on the emulator */ +internal fun TerminalEmulator.useMachine(m: ByteMachine) { + requestedColumns = m.columns + requestedRows = m.rows + val p = m.palette + for (i in p.indices) setColorOverride(i, p[i]) + setColorOverride(256, p[m.defaultColor]) + setColorOverride(257, p[m.background]) +} + +/** the view's size and the theme's colours back after [useMachine], the style plain */ +internal fun TerminalEmulator.releaseMachine() { + requestedColumns = 0 + requestedRows = 0 + resetColors(0..257) + style = Style.NORMAL +} + /** * The machine a [ByteControlInterpreter] is: its screen's size and its * palette (ARGB, in its own colour order), the colour it writes in and diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/LegacyCharsets.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/LegacyCharsets.kt index a78fc4d..9f5db59 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/LegacyCharsets.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/LegacyCharsets.kt @@ -7,7 +7,7 @@ package org.dmodem.app.terminal * The 8-bit home computers' character sets, each code (0x00..0xff) as its * Unicode code point (0: not a character), from Rebecca Bettencourt's * Legacy Computing mappings; and [GLYPHS], an 8x8 glyph for every one of - * them, and [VGA], code page 437 in 8x16 as a PC's VGA showed it; both + * them (and Latin-1's, an Amiga's), and [VGA], code page 437 in 8x16 as a PC's VGA showed it; both * from Unscii (public domain; the few it hasn't drawn in the generator). */ internal object LegacyCharsets { @@ -326,34 +326,51 @@ internal object LegacyCharsets { "74:30307E3030301E00;75:0000666666663E00;76:00006666663C1800;77:0000C6C6D67C6C00;" + "78:0000C66C386CC600;79:00006666663E063C;7A:00007E0C18307E00;7B:0E18187018180E00;" + "7C:1818181818181800;7D:7018180E18187000;7E:76DC000000000000;A0:0000000000000000;" + - "A1:1800181818181800;A3:386C60F06066FC00;C4:66003C667E666600;C5:1818003C667E6600;" + - "C9:0E00FEC0F8C0FE00;D1:76DC00C6F6DEC600;D6:66007CC6C6C67C00;DC:6600C6C6C6C67C00;" + - "E0:70003C063E663E00;E1:0E003C063E663E00;E2:1866003E66C67E00;E4:66003C063E663E00;" + - "E5:1818003E66C67E00;E7:00003C6060603C18;E8:70003C667E603C00;E9:0E003C667E603C00;" + - "EA:1866003C7E603C00;EC:7000381818183C00;EE:1866003818183C00;EF:6600381818183C00;" + + "A1:1800181818181800;A2:00187ED8D87E1800;A3:386C60F06066FC00;A4:663C663C66000000;" + + "A5:C3663C183C181800;A6:1818180018181800;A7:3C603C66663C063C;A8:6600000000000000;" + + "A9:7E819DB1B19D817E;AA:3C6C6C3E007E0000;AB:003366CC66330000;AC:007E060600000000;" + + "AD:0000003C00000000;AE:7E81B9A5B9A5817E;AF:7E00000000000000;B0:3C663C0000000000;" + + "B1:18187E1818007E00;B2:7018306078000000;B3:780C180C78000000;B4:0C18300000000000;" + + "B5:00006666667C60C0;B6:3E7A7A3A1A1A1A00;B7:0000001800000000;B8:0000000000000818;" + + "B9:3070303030000000;BA:386C6C38007C0000;BB:00CC663366CC0000;BC:40C64C5A366ACF02;" + + "BD:40C64C5E3366CC0F;BE:C0266C3AF66ACF02;BF:1800183060663C00;C0:70003C667E666600;" + + "C1:0E003C667E666600;C2:1866003C667E6600;C3:76DC003C667E6600;C4:66003C667E666600;" + + "C5:1818003C667E6600;C6:3F6CCCFECCCCCF00;C7:3C66606060663C18;C8:7000FEC0F8C0FE00;" + + "C9:0E00FEC0F8C0FE00;CA:186600FEF0C0FE00;CB:6600FEC0F8C0FE00;CC:70007E1818187E00;" + + "CD:0E007E1818187E00;CE:1866007E18187E00;CF:66007E1818187E00;D0:786C66F6666C7800;" + + "D1:76DC00C6F6DEC600;D2:70007CC6C6C67C00;D3:0E007CC6C6C67C00;D4:1866007CC6C67C00;" + + "D5:76DC007CC6C67C00;D6:66007CC6C6C67C00;D7:00C66C386CC60000;D8:3E666E7E76667C00;" + + "D9:7000C6C6C6C67C00;DA:0E00C6C6C6C67C00;DB:186600C6C6C67C00;DC:6600C6C6C6C67C00;" + + "DD:0E0066663C181800;DE:C0C0FCC6FCC0C000;DF:3C66666C66666C00;E0:70003C063E663E00;" + + "E1:0E003C063E663E00;E2:1866003E66C67E00;E3:76DC003E66C67E00;E4:66003C063E663E00;" + + "E5:1818003E66C67E00;E6:00007E1B7FD87700;E7:00003C6060603C18;E8:70003C667E603C00;" + + "E9:0E003C667E603C00;EA:1866003C7E603C00;EB:66003C667E603C00;EC:7000381818183C00;" + + "ED:0E00381818183C00;EE:1866003818183C00;EF:6600381818183C00;F0:0C3E0C7CCCCC7800;" + "F1:76DC007C66666600;F2:70003C6666663C00;F3:0E003C6666663C00;F4:1866003C66663C00;" + - "F6:66003C6666663C00;F9:7000666666663E00;FA:0E00666666663E00;FB:1866006666663E00;" + - "FC:6600666666663E00;3C0:0000FF6666666600;2022:0000183C3C180000;2190:0010307F7F301000;" + - "2191:183C7E1818181818;2192:00080CFEFE0C0800;2193:18181818187E3C18;23B8:C0C0C0C0C0C0C0C0;" + - "23B9:0303030303030303;23BA:FFFF000000000000;23BD:0000000000FFFF00;241B:007E605048444200;" + - "2500:000000FFFF000000;2502:1818181818181818;250C:0000001F1F181818;2510:000000F8F8181818;" + - "2514:1818181F1F000000;2518:181818F8F8000000;251C:1818181F1F181818;2524:181818F8F8181818;" + - "252C:000000FFFF181818;2534:181818FFFF000000;253C:181818FFFF181818;256D:000000070F1C1818;" + - "256E:000000E0F0381818;256F:181838F0E0000000;2570:18181C0F07000000;2571:03060C183060C080;" + - "2572:C06030180C060301;2573:C3663C183C66C381;2581:00000000000000FF;2582:000000000000FFFF;" + - "2583:0000000000FFFFFF;2584:00000000FFFFFFFF;258C:F0F0F0F0F0F0F0F0;258D:E0E0E0E0E0E0E0E0;" + - "258E:C0C0C0C0C0C0C0C0;258F:8080808080808080;2592:AA55AA55AA55AA55;2594:FF00000000000000;" + - "2595:0101010101010101;2596:00000000F0F0F0F0;2597:000000000F0F0F0F;2598:F0F0F0F000000000;" + - "259A:F0F0F0F00F0F0F0F;259D:0F0F0F0F00000000;25B6:C0F0FCFFFFFCF0C0;25C0:030F3FFFFF3F0F03;" + - "25CB:003C664242663C00;25E2:0103070F1F3F7FFF;25E3:80C0E0F0F8FCFEFF;25E4:FFFEFCF8F0E0C080;" + - "25E5:FF7F3F1F0F070301;2660:10387CFEFE387C00;2663:1818666618183C00;2665:6CFEFEFE7C381000;" + - "2666:10387CFE7C381000;2713:02060CD8F0E0C000;1F8B0:0078605048040200;1FB70:6060606060606060;" + - "1FB71:3030303030303030;1FB72:1818181818181818;1FB74:0C0C0C0C0C0C0C0C;1FB75:0606060606060606;" + - "1FB76:00FFFF0000000000;1FB77:0000FFFF00000000;1FB78:000000FFFF000000;1FB7A:00000000FFFF0000;" + - "1FB7B:0000000000FFFF00;1FB7C:C0C0C0C0C0C0C0FF;1FB7D:FFC0C0C0C0C0C0C0;1FB7E:FF03030303030303;" + - "1FB7F:03030303030303FF;1FB82:FFFF000000000000;1FB83:FFFFFF0000000000;1FB87:0303030303030303;" + - "1FB88:0707070707070707;1FB8C:A050A050A050A050;1FB8F:00000000AA55AA55;1FB95:CCCC3333CCCC3333;" + - "1FB96:3333CCCC3333CCCC;1FB98:CC663399CC663399;1FB99:3366CC993366CC99;" + + "F5:76DC003C66663C00;F6:66003C6666663C00;F7:1818007E00181800;F8:00027CCED6E67C80;" + + "F9:7000666666663E00;FA:0E00666666663E00;FB:1866006666663E00;FC:6600666666663E00;" + + "FD:0E006666663E063C;FE:60607C66667C6060;FF:66006666663E063C;3C0:0000FF6666666600;" + + "2022:0000183C3C180000;2190:0010307F7F301000;2191:183C7E1818181818;2192:00080CFEFE0C0800;" + + "2193:18181818187E3C18;23B8:C0C0C0C0C0C0C0C0;23B9:0303030303030303;23BA:FFFF000000000000;" + + "23BD:0000000000FFFF00;241B:007E605048444200;2500:000000FFFF000000;2502:1818181818181818;" + + "250C:0000001F1F181818;2510:000000F8F8181818;2514:1818181F1F000000;2518:181818F8F8000000;" + + "251C:1818181F1F181818;2524:181818F8F8181818;252C:000000FFFF181818;2534:181818FFFF000000;" + + "253C:181818FFFF181818;256D:000000070F1C1818;256E:000000E0F0381818;256F:181838F0E0000000;" + + "2570:18181C0F07000000;2571:03060C183060C080;2572:C06030180C060301;2573:C3663C183C66C381;" + + "2581:00000000000000FF;2582:000000000000FFFF;2583:0000000000FFFFFF;2584:00000000FFFFFFFF;" + + "258C:F0F0F0F0F0F0F0F0;258D:E0E0E0E0E0E0E0E0;258E:C0C0C0C0C0C0C0C0;258F:8080808080808080;" + + "2592:AA55AA55AA55AA55;2594:FF00000000000000;2595:0101010101010101;2596:00000000F0F0F0F0;" + + "2597:000000000F0F0F0F;2598:F0F0F0F000000000;259A:F0F0F0F00F0F0F0F;259D:0F0F0F0F00000000;" + + "25B6:C0F0FCFFFFFCF0C0;25C0:030F3FFFFF3F0F03;25CB:003C664242663C00;25E2:0103070F1F3F7FFF;" + + "25E3:80C0E0F0F8FCFEFF;25E4:FFFEFCF8F0E0C080;25E5:FF7F3F1F0F070301;2660:10387CFEFE387C00;" + + "2663:1818666618183C00;2665:6CFEFEFE7C381000;2666:10387CFE7C381000;2713:02060CD8F0E0C000;" + + "1F8B0:0078605048040200;1FB70:6060606060606060;1FB71:3030303030303030;1FB72:1818181818181818;" + + "1FB74:0C0C0C0C0C0C0C0C;1FB75:0606060606060606;1FB76:00FFFF0000000000;1FB77:0000FFFF00000000;" + + "1FB78:000000FFFF000000;1FB7A:00000000FFFF0000;1FB7B:0000000000FFFF00;1FB7C:C0C0C0C0C0C0C0FF;" + + "1FB7D:FFC0C0C0C0C0C0C0;1FB7E:FF03030303030303;1FB7F:03030303030303FF;1FB82:FFFF000000000000;" + + "1FB83:FFFFFF0000000000;1FB87:0303030303030303;1FB88:0707070707070707;1FB8C:A050A050A050A050;" + + "1FB8F:00000000AA55AA55;1FB95:CCCC3333CCCC3333;1FB96:3333CCCC3333CCCC;1FB98:CC663399CC663399;" + + "1FB99:3366CC993366CC99;" + "", ) diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Raster.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Raster.kt index 63c5afa..23868ae 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Raster.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Raster.kt @@ -29,6 +29,12 @@ class Raster(val width: Int, val height: Int, private val logicalWidth: Int, pri var background = 0 /** the line pattern: 8 bits, the first pixel the high one (0xff solid) */ var pattern = 0xff + /** [fill]'s pattern: 8 rows of 8 bits (high bit left), the colour where set and [fillBackground] where not; null solid */ + var fillPattern: IntArray? = null + /** what a [fillPattern]'s clear bits are filled with (0: left as they are) */ + var fillBackground = 0 + /** the pixels drawing may touch (left, top, right, bottom, inclusive); null all */ + var clip: IntArray? = null private var patternStep = 0 /** changed since [take] */ var dirty = false @@ -49,6 +55,7 @@ class Raster(val width: Int, val height: Int, private val logicalWidth: Int, pri /** pixel ([x], [y]) written as the mode says ([on]: the pattern's bit there) */ private fun put(x: Int, y: Int, on: Boolean = true) { if (x !in 0 until width || y !in 0 until height) return + clip?.let { c -> if (x < c[0] || y < c[1] || x > c[2] || y > c[3]) return } val i = y * width + x when (mode) { Mode.OVERLAY -> if (on) pixels[i] = color @@ -95,14 +102,33 @@ class Raster(val width: Int, val height: Int, private val logicalWidth: Int, pri /** an arc round logical ([cx], [cy]), radius [r], from [start] degrees (0 east, counterclockwise) through [sweep] */ fun arc(cx: Int, cy: Int, r: Int, start: Double, sweep: Double) { - val steps = maxOf(16, (abs(sweep) / 360 * maxOf(8, px(abs(r)) * 2) * PI).toInt()) + // (the same radius in pixels both ways: round on any screen) + pixelArc(px(cx), py(cy), px(r).toDouble(), px(r).toDouble(), start, sweep) + } + + /** an elliptical arc round logical ([cx], [cy]), radii [rx] and [ry] (each in its own axis' units), as [arc] */ + fun ellipse(cx: Int, cy: Int, rx: Int, ry: Int, start: Double, sweep: Double) { + pixelArc(px(cx), py(cy), rx.toDouble() * width / logicalWidth, ry.toDouble() * height / logicalHeight, start, sweep) + } + + /** an elliptical arc's points (logical), [start] degrees through [sweep], for a polygon (a pie's or a filled ellipse's) */ + fun ellipsePoints(cx: Int, cy: Int, rx: Int, ry: Int, start: Double, sweep: Double): List> { + val n = maxOf(16, (abs(sweep) / 360 * 64).toInt()) + return (0..n).map { i -> + val a = (start + sweep * i / n) * PI / 180 + (cx + (rx * cos(a)).roundToInt()) to (cy - (ry * sin(a)).roundToInt()) + } + } + + /** an arc of pixels round ([cx], [cy]), radii [rx] and [ry] in pixels, by short lines */ + private fun pixelArc(cx: Int, cy: Int, rx: Double, ry: Double, start: Double, sweep: Double) { + val steps = maxOf(16, (abs(sweep) / 360 * maxOf(8.0, maxOf(rx, ry) * 2) * PI).toInt()) var lx = 0 var ly = 0 for (i in 0..steps) { val a = (start + sweep * i / steps) * PI / 180 - val x = px(cx) + (px(r) * cos(a)).roundToInt() - // (the same radius in pixels both ways: round on any screen) - val y = py(cy) - (px(r) * sin(a)).roundToInt() + val x = cx + (rx * cos(a)).roundToInt() + val y = cy - (ry * sin(a)).roundToInt() if (i > 0) pixelLine(lx, ly, x, y) lx = x ly = y @@ -125,10 +151,97 @@ class Raster(val width: Int, val height: Int, private val logicalWidth: Int, pri if (y >= minOf(y0, y1) && y < maxOf(y0, y1)) xs.add(x0 + (y - y0) * (x1 - x0) / (y1 - y0)) } xs.sort() - for (k in 0 until xs.size - 1 step 2) for (x in xs[k]..xs[k + 1]) put(x, y) + for (k in 0 until xs.size - 1 step 2) for (x in xs[k]..xs[k + 1]) fillPixel(x, y) } } + /** a filled area's pixel: the colour, or where [fillPattern]'s bit is clear [fillBackground] (if any) */ + private fun fillPixel(x: Int, y: Int) { + val p = fillPattern ?: return put(x, y) + if ((p[y and 7] shr (7 - (x and 7))) and 1 != 0) { + put(x, y) + } else if (fillBackground != 0) { + val c = color + color = fillBackground + put(x, y) + color = c + } + } + + /** the area round logical ([x], [y]) up to pixels of colour [border] filled (as [fill] fills), a scan line at a time */ + fun floodFill(x: Int, y: Int, border: Int) { + val sx = px(x) + val sy = py(y) + if (sx !in 0 until width || sy !in 0 until height || pixels[sy * width + sx] == border) return + val seen = BooleanArray(width * height) + val stack = ArrayDeque() + stack.addLast(sy * width + sx) + /** whether the pixel at [i] is inside: not the border, not filled yet, in the clip */ + fun inside(i: Int): Boolean { + if (seen[i] || pixels[i] == border) return false + val c = clip ?: return true + val xx = i % width + val yy = i / width + return xx >= c[0] && yy >= c[1] && xx <= c[2] && yy <= c[3] + } + while (stack.isNotEmpty()) { + val i = stack.removeLast() + if (!inside(i)) continue + val row = i / width + var l = i % width + var r = l + while (l > 0 && inside(row * width + l - 1)) l-- + while (r < width - 1 && inside(row * width + r + 1)) r++ + for (xx in l..r) seen[row * width + xx] = true + for (xx in l..r) fillPixel(xx, row) + for (ny in intArrayOf(row - 1, row + 1)) { + if (ny !in 0 until height) continue + for (xx in l..r) if (inside(ny * width + xx)) stack.addLast(ny * width + xx) + } + } + dirty = true + } + + /** a copy of the pixels in logical ([x0], [y0])-([x1], [y1]) */ + fun grab(x0: Int, y0: Int, x1: Int, y1: Int): Grab { + val l = px(minOf(x0, x1)).coerceIn(0, width - 1) + val t = py(minOf(y0, y1)).coerceIn(0, height - 1) + val r = px(maxOf(x0, x1) + 1).coerceIn(l + 1, width) + val b = py(maxOf(y0, y1) + 1).coerceIn(t + 1, height) + val w = r - l + val h = b - t + val out = IntArray(w * h) + for (yy in 0 until h) pixels.copyInto(out, yy * w, (t + yy) * width + l, (t + yy) * width + l + w) + return Grab(w, h, out) + } + + /** a [grab]'s pixels put with their top left at logical ([x], [y]): copied, or [xor]ed over what's there */ + fun put(g: Grab, x: Int, y: Int, xor: Boolean = false) { + val l = px(x) + val t = py(y) + for (yy in 0 until g.height) for (xx in 0 until g.width) { + val dx = l + xx + val dy = t + yy + if (dx !in 0 until width || dy !in 0 until height) continue + clip?.let { c -> if (dx < c[0] || dy < c[1] || dx > c[2] || dy > c[3]) continue } + val v = g.pixels[yy * g.width + xx] + val i = dy * width + dx + pixels[i] = if (xor) (pixels[i] xor v) or (if (pixels[i] or v != 0) 0xff shl 24 else 0) else v + } + dirty = true + } + + /** logical ([x0], [y0])-([x1], [y1]) as pixels (left, top, right, bottom, inclusive), for [clip] */ + fun pixelRect(x0: Int, y0: Int, x1: Int, y1: Int): IntArray = + intArrayOf(px(minOf(x0, x1)), py(minOf(y0, y1)), px(maxOf(x0, x1) + 1) - 1, py(maxOf(y0, y1) + 1) - 1) + + /** the pixels in logical ([x0], [y0])-([x1], [y1]) made [c] (0: transparent) */ + fun clearRect(x0: Int, y0: Int, x1: Int, y1: Int, c: Int = 0) { + val r = pixelRect(x0, y0, x1, y1) + for (yy in maxOf(0, r[1])..minOf(height - 1, r[3])) for (xx in maxOf(0, r[0])..minOf(width - 1, r[2])) pixels[yy * width + xx] = c + dirty = true + } + /** * [text] with its top left at logical ([x], [y]), each character * [cw] x [ch] logical units drawn from [font] (its glyph scaled to the @@ -154,15 +267,25 @@ class Raster(val width: Int, val height: Int, private val logicalWidth: Int, pri val w = maxOf(1, px(x + cw) - left) val h = maxOf(1, py(y + ch) - top) for (yy in 0 until h) { - val row = font.row(cp, yy * font.height / h) + // (shrunk: the glyph's rows this one covers merged, so a thin stroke isn't lost) + val r0 = yy * font.height / h + val r1 = maxOf(r0 + 1, (yy + 1) * font.height / h) + var row = 0 + for (r in r0 until r1) row = row or font.row(cp, r) if (row == 0) continue for (xx in 0 until w) { - val col = xx * font.width / w - if ((row shr (font.width - 1 - col)) and 1 != 0) put(left + xx, top + yy) + val c0 = xx * font.width / w + val c1 = maxOf(c0 + 1, (xx + 1) * font.width / w) + var on = false + for (col in c0 until c1) if ((row shr (font.width - 1 - col)) and 1 != 0) on = true + if (on) put(left + xx, top + yy) } } } + /** pixels [grab]bed from a raster: [width] x [height] of them, row by row */ + class Grab(val width: Int, val height: Int, val pixels: IntArray) + /** the picture as an image whose tiles are [cellWidth] x [cellHeight] (the screen's cells), not dirty after */ fun take(cellWidth: Int, cellHeight: Int): TermImage { dirty = false diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Rip.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Rip.kt new file mode 100644 index 0000000..836b38b --- /dev/null +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Rip.kt @@ -0,0 +1,567 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +import kotlin.math.roundToInt + +/** + * RIPscrip 1.54, TeleGrafix's graphics for BBSes: ANSI-BBS whose lines + * that start with !| are drawing commands on a 640x350 EGA screen + * (lines, rectangles, circles, ellipses, arcs, pies, Béziers, polygons, + * flood and patterned fills, text, the clipboard) and mouse regions and + * buttons that a tap sends the host. Ordinary text and ANSI go to the + * text window, 80x43 of 8x8 cells. + */ +open class RipProfile : AnsiBbsProfile() { + override val id get() = "rip" + override val label get() = "RIPscrip" + override val term get() = "rip" + // (BBSes find ANSI by it, then ask for RIP with ESC [ !) + override val telnetType get() = "ANSI" + + /** a [RipInterpreter] */ + override fun interpreter(t: TerminalEmulator): TerminalInterpreter = RipInterpreter(t, this) +} + +/** RIPscrip's commands taken from the lines that start with !| (or ^A|, ^B|), the rest ANSI-BBS's */ +open class RipInterpreter(t: TerminalEmulator, profile: RipProfile) : AnsiBbsInterpreter(t, profile) { + private val layer = PictureLayer(t, WIDTH, HEIGHT) + private val raster get() = layer.raster + + // reading: a line's start, a RIP line being gathered, an ESC [ … ! being held + private var lineStart = true + private var bang = false + private var line: StringBuilder? = null + private var escaped = false + /** a CR ended a RIP line (or continued one): the LF after it is the line's too */ + private var swallowLf = false + private val held = StringBuilder() + /** the characters being given back to ANSI-BBS's parser (not taken again) */ + private var replaying = false + /** RIP on (ESC [ 2 !) or off (ESC [ 1 !) */ + private var enabled = true + + // the drawing state + private var color = 15 + private var x = 0 + private var y = 0 + private var xor = false + private var linePattern = 0xff + private var thick = 1 + private var fillPattern = 1 + private var userFill = IntArray(8) { 0xff } + private var fillColor = 15 + private var font = 0 + private var fontSize = 1 + private var vertical = false + private val palette = EGA.copyOf() + private var viewport: IntArray? = null + private var clipboard: Raster.Grab? = null + + /** a mouse region or button: where it is and what a tap on it sends */ + private class Region(val x0: Int, val y0: Int, val x1: Int, val y1: Int, val send: String) + private val regions = ArrayList() + + /** the buttons' look (1B): their colours and bevel */ + private class ButtonStyle(val bevel: Int, val fore: Int, val surface: Int, val bright: Int, val dark: Int) + private var buttonStyle = ButtonStyle(2, 0, 7, 15, 8) + + /** a text block (1T) under way: its corners and the next line's top */ + private var block: IntArray? = null + + init { + textScreen() + } + + /** the text window's 80x43 of 8x8 cells */ + private fun textScreen() { + t.requestedColumns = 80 + t.requestedRows = 43 + } + + /** a code point: a RIP line's, an ESC [ … ! query's, else ANSI-BBS's */ + override fun intercept(cp: Int): Boolean { + if (replaying) return super.intercept(cp) + if (swallowLf) { + swallowLf = false + if (cp == 0x0a) return true + } + val l = line + if (l != null) { + gather(l, cp) + return true + } + if (held.isNotEmpty() || cp == 0x1b) return query(cp) + if (bang) { + bang = false + if (cp == '|'.code) { + line = StringBuilder() + return true + } + giveBack("!") + } + if (enabled && lineStart && (cp == '!'.code || cp == 0x01 || cp == 0x02)) { + bang = true + return true + } + lineStart = cp == 0x0d || cp == 0x0a + return super.intercept(cp) + } + + /** [s] given to ANSI-BBS's parser after all */ + private fun giveBack(s: String) { + replaying = true + for (c in s) process(c.code) + replaying = false + lineStart = false + } + + /** a RIP line's character: kept (a backslash's escape or continuation honoured) until the line ends, then run */ + private fun gather(l: StringBuilder, cp: Int) { + if (escaped) { + escaped = false + when (cp) { + 0x0d -> swallowLf = true + 0x0a -> {} + else -> { l.append('\\'); l.appendCp(cp) } + } + return + } + when (cp) { + '\\'.code -> escaped = true + 0x0d, 0x0a -> { + line = null + lineStart = true + swallowLf = cp == 0x0d + runLine(l.toString()) + } + else -> if (l.length < MAX_LINE) l.appendCp(cp) + } + } + + /** ESC [ !, ESC [ 0 !, 1 !, 2 !: held until it is one (answered: RIP's version; off; on), else given back */ + private fun query(cp: Int): Boolean { + held.appendCp(cp) + val h = held.toString() + val full = h.length >= 3 && h.endsWith("!") && h.startsWith("\u001b[") && h.substring(2, h.length - 1).all { it in '0'..'2' } + if (full) { + held.setLength(0) + when (h.substring(2, h.length - 1)) { + "", "0" -> t.reply("RIPSCRIP015400") + "1" -> enabled = false + "2" -> enabled = true + } + return true + } + val partial = h == "\u001b" || h == "\u001b[" || (h.length == 3 && h[2] in '0'..'2') + if (partial) return true + held.setLength(0) + giveBack(h) + return true + } + + /** a RIP line's commands, split at the |s that aren't escaped, each run */ + private fun runLine(s: String) { + val cmd = StringBuilder() + var i = 0 + while (i < s.length) { + val c = s[i] + if (c == '\\' && i + 1 < s.length) { + cmd.append(s[i + 1]) + i += 2 + continue + } + if (c == '|') { + run(cmd.toString()) + cmd.setLength(0) + } else { + cmd.append(c) + } + i++ + } + run(cmd.toString()) + } + + /** one command: its level (a digit) and letter, then its MegaNums and text */ + private fun run(cmd: String) { + if (cmd.isEmpty()) return + val level = if (cmd[0] in '1'..'9') cmd[0] - '0' else 0 + val at = if (level > 0) 1 else 0 + if (at >= cmd.length) return + val a = Args(cmd, at + 1) + if (level == 1) level1(cmd[at], a) else if (level == 0) level0(cmd[at], a) + } + + /** the level 0 commands: windows, colours, drawing, text, styles */ + private fun level0(c: Char, a: Args) { + when (c) { + 'w' -> textWindow(a.n(), a.n(), a.n(), a.n()) + 'v' -> { + val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); val y1 = a.n() + viewport = intArrayOf(x0, y0, x1, y1) + } + '*' -> resetWindows() + 'e' -> { + t.eraseDisplay(2) + t.moveCursor(0, t.top) + } + 'E' -> viewport.let { v -> if (v == null) raster.clearRect(0, 0, WIDTH - 1, HEIGHT - 1) else raster.clearRect(v[0], v[1], v[2], v[3]) } + 'g' -> { val gx = a.n(); val gy = a.n(); t.moveCursor(gx, t.top + gy) } + 'H' -> t.moveCursor(0, t.top) + '>' -> t.eraseLine(0) + 'c' -> color = a.n() and 15 + 'Q' -> for (i in 0 until 16) palette[i] = ega(a.n()) + 'a' -> { val i = a.n(); val v = a.n(); if (i in 0..15) palette[i] = ega(v) } + 'W' -> xor = a.n() == 1 + 'm' -> { x = a.n(); y = a.n() } + 'T' -> text(a.rest()) + '@' -> { x = a.n(); y = a.n(); text(a.rest()) } + 'Y' -> { font = a.n(); vertical = a.n() == 1; fontSize = a.n().coerceIn(1, 10) } + 'X' -> { setUp(); raster.point(a.n(), a.n()) } + 'L' -> { val x0 = a.n(); val y0 = a.n(); line(x0, y0, a.n(), a.n()) } + 'R' -> { + val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); val y1 = a.n() + polyline(listOf(x0 to y0, x1 to y0, x1 to y1, x0 to y1, x0 to y0)) + } + 'B' -> { + val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); val y1 = a.n() + fill(listOf(x0 to y0, x1 + 1 to y0, x1 + 1 to y1 + 1, x0 to y1 + 1)) + } + 'C' -> { val cx = a.n(); val cy = a.n(); val r = a.n(); setUp(); raster.ellipse(cx, cy, r, aspect(r), 0.0, 360.0) } + 'A' -> { val cx = a.n(); val cy = a.n(); val s = a.n(); val e = a.n(); val r = a.n(); arc(cx, cy, s, e, r, aspect(r)) } + 'O', 'V' -> { val cx = a.n(); val cy = a.n(); val s = a.n(); val e = a.n(); arc(cx, cy, s, e, a.n(), a.n()) } + 'o' -> { + val cx = a.n(); val cy = a.n(); val rx = a.n(); val ry = a.n() + fill(raster.ellipsePoints(cx, cy, rx, ry, 0.0, 360.0)) + setUp() + raster.ellipse(cx, cy, rx, ry, 0.0, 360.0) + } + 'I' -> { val cx = a.n(); val cy = a.n(); val s = a.n(); val e = a.n(); val r = a.n(); pie(cx, cy, s, e, r, aspect(r)) } + 'i' -> { val cx = a.n(); val cy = a.n(); val s = a.n(); val e = a.n(); pie(cx, cy, s, e, a.n(), a.n()) } + 'Z' -> { + val p = List(4) { a.n() to a.n() } + bezier(p, a.n().coerceIn(1, 256)) + } + 'P', 'p', 'l' -> { + val n = a.n() + val pts = List(n) { a.n() to a.n() } + if (pts.size < 2) return + if (c == 'p') fill(pts) + polyline(if (c == 'l') pts else pts + pts[0]) + } + 'F' -> { val fx = a.n(); val fy = a.n(); val border = a.n(); setUpFill(); raster.floodFill(fx, fy, argb(border)) } + '=' -> { + val style = a.n() + val user = a.n(4) + thick = if (a.n() >= 3) 3 else 1 + linePattern = when (style) { + 1 -> 0xcc + 2 -> 0xe4 + 3 -> 0xf0 + 4 -> (user shr 8) and 0xff + else -> 0xff + } + } + 'S' -> { fillPattern = a.n(); fillColor = a.n() and 15 } + 's' -> { userFill = IntArray(8) { a.n() and 0xff }; fillColor = a.n() and 15; fillPattern = 12 } + } + } + + /** the level 1 commands: mouse regions, buttons, text blocks, the clipboard, file queries */ + private fun level1(c: Char, a: Args) { + when (c) { + 'M' -> { + a.n() + val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); val y1 = a.n() + a.n(); a.n(1); a.n(5) + regions.add(Region(x0, y0, x1, y1, a.rest())) + } + 'K' -> regions.clear() + 'B' -> { + a.n(); a.n(); a.n(); a.n(4) + val bevel = a.n(); val fore = a.n(); a.n() + val bright = a.n(); val dark = a.n(); val surface = a.n() + buttonStyle = ButtonStyle(bevel, fore and 15, surface and 15, bright and 15, dark and 15) + } + 'U' -> { + val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); val y1 = a.n() + a.n(); a.n(1); a.n(1) + button(x0, y0, x1, y1, a.rest()) + } + 'T' -> { val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); block = intArrayOf(x0, y0, x1, a.n(), y0) } + 't' -> block?.let { b -> + a.n(1) + x = b[0] + y = b[4] + text(a.rest()) + b[4] += cellHeight() + } + 'E' -> block = null + 'C' -> { val x0 = a.n(); val y0 = a.n(); clipboard = raster.grab(x0, y0, a.n(), a.n()) } + 'P' -> clipboard?.let { g -> val px = a.n(); val py = a.n(); val mode = a.n(); raster.put(g, px, py, xor = mode == 1) } + 'G' -> { + val x0 = a.n(); val y0 = a.n(); val x1 = a.n(); val y1 = a.n() + a.n() + val dest = a.n() + raster.put(raster.grab(x0, y0, x1, y1), x0, dest) + } + // a file query: none of the host's icons are here + 'F' -> t.reply("0\r\n") + } + } + + /** RIP's text window: rows [y0] to [y1] (of 43) scroll, the cursor at its top */ + private fun textWindow(x0: Int, y0: Int, x1: Int, y1: Int) { + if (x0 == 0 && y0 == 0 && x1 == 0 && y1 == 0) return + val top = y0.coerceIn(0, t.rows - 1) + val bottom = y1.coerceIn(top, t.rows - 1) + t.setRegion(top, bottom) + t.moveCursor(x0.coerceIn(0, t.cols - 1), top) + } + + /** RIP_RESET_WINDOWS: the picture, the text, the windows, the palette, the regions as at the start */ + private fun resetWindows() { + layer.reset() + t.setRegion(0, t.rows - 1) + t.eraseDisplay(2) + t.moveCursor(0, 0) + EGA.copyInto(palette) + viewport = null + regions.clear() + clipboard = null + color = 15 + x = 0 + y = 0 + } + + /** colour register [c] as drawn: its colour, black transparent (the text shows through) */ + private fun argb(c: Int): Int { + val v = palette[c and 15] + return if (v and 0xffffff == 0) 0 else v + } + + /** the raster set to draw lines: the colour, xor, the line style, the viewport */ + private fun setUp() { + val r = raster + r.mode = if (xor) Raster.Mode.COMPLEMENT else Raster.Mode.OVERLAY + r.color = argb(color) + r.pattern = linePattern + r.fillPattern = null + r.clip = viewport?.let { r.pixelRect(it[0], it[1], it[2], it[3]) } + if (r.color == 0) r.mode = Raster.Mode.ERASE + r.background = 0 + } + + /** the raster set to fill: the fill colour and pattern (BGI's twelve, or the user's) */ + private fun setUpFill() { + setUp() + val r = raster + r.color = argb(fillColor) + r.mode = if (r.color == 0) Raster.Mode.ERASE else Raster.Mode.OVERLAY + r.fillPattern = when (fillPattern) { + 0 -> IntArray(8) + 1 -> null + 12 -> userFill + else -> FILLS.getOrNull(fillPattern - 2) + } + r.fillBackground = 0 + } + + /** a line in the line style, three pixels thick if it's thick */ + private fun line(x0: Int, y0: Int, x1: Int, y1: Int) { + setUp() + raster.line(x0, y0, x1, y1) + if (thick == 3) { + raster.line(x0 + 1, y0, x1 + 1, y1) + raster.line(x0, y0 + 1, x1, y1 + 1) + } + } + + /** lines through [pts] */ + private fun polyline(pts: List>) { + for (i in 1 until pts.size) line(pts[i - 1].first, pts[i - 1].second, pts[i].first, pts[i].second) + } + + /** the polygon through [pts] filled in the fill style */ + private fun fill(pts: List>) { + setUpFill() + raster.fill(pts) + } + + /** an elliptical arc from [s] to [e] degrees */ + private fun arc(cx: Int, cy: Int, s: Int, e: Int, rx: Int, ry: Int) { + setUp() + raster.ellipse(cx, cy, rx, ry, s.toDouble(), sweep(s, e)) + } + + /** a pie slice from [s] to [e] degrees, filled and outlined */ + private fun pie(cx: Int, cy: Int, s: Int, e: Int, rx: Int, ry: Int) { + val pts = listOf(cx to cy) + raster.ellipsePoints(cx, cy, rx, ry, s.toDouble(), sweep(s, e)) + (cx to cy) + fill(pts) + polyline(pts) + } + + /** the degrees counterclockwise from [s] to [e] */ + private fun sweep(s: Int, e: Int): Double = (((e - s) % 360 + 360) % 360).let { if (it == 0) 360 else it }.toDouble() + + /** a circle's vertical radius: radius [r] as the EGA's tall pixels have it */ + private fun aspect(r: Int): Int = (r * ASPECT).roundToInt() + + /** a cubic Bézier through control points [p] in [n] lines */ + private fun bezier(p: List>, n: Int) { + val pts = (0..n).map { i -> + val u = i.toDouble() / n + val v = 1 - u + /** one coordinate at u */ + fun c(a: Int, b: Int, cc: Int, d: Int) = (v * v * v * a + 3 * v * v * u * b + 3 * v * u * u * cc + u * u * u * d).roundToInt() + c(p[0].first, p[1].first, p[2].first, p[3].first) to c(p[0].second, p[1].second, p[2].second, p[3].second) + } + polyline(pts) + } + + /** a character cell's height for the font and size */ + private fun cellHeight(): Int = if (font == 0) 8 * fontSize else 10 * fontSize + + /** [s] drawn from the position in the font (the PC's 8x8 cells, or bigger for the stroke fonts' sizes), the position after it */ + private fun text(s: String) { + setUp() + raster.mode = if (xor) Raster.Mode.COMPLEMENT else Raster.Mode.OVERLAY + val cw = if (font == 0) 8 * fontSize else 6 * fontSize + val ch = cellHeight() + for (c in s) { + raster.text(x, y, c.toString(), LegacyCharsets.VGA, cw, ch) + if (vertical) y += ch else x += cw + } + } + + /** a button (1U): its bevel and surface in the buttons' style, its label, and a region sending its host text ("icon<>label<>host") */ + private fun button(x0: Int, y0: Int, x1: Int, y1: Int, spec: String) { + val parts = spec.split("<>") + val label = parts.getOrElse(1) { "" } + val host = parts.getOrElse(2) { "" } + val st = buttonStyle + val saved = fillColor to fillPattern + fillColor = st.surface + fillPattern = 1 + fill(listOf(x0 to y0, x1 + 1 to y0, x1 + 1 to y1 + 1, x0 to y1 + 1)) + fillColor = saved.first + fillPattern = saved.second + val savedColor = color + for (b in 0 until st.bevel) { + color = st.bright + polyline(listOf(x0 - b - 1 to y1 + b + 1, x0 - b - 1 to y0 - b - 1, x1 + b + 1 to y0 - b - 1)) + color = st.dark + polyline(listOf(x1 + b + 1 to y0 - b - 1, x1 + b + 1 to y1 + b + 1, x0 - b - 1 to y1 + b + 1)) + } + color = st.fore + val savedFont = Triple(font, fontSize, vertical) + font = 0 + fontSize = 1 + vertical = false + x = (x0 + x1) / 2 - label.length * 4 + y = (y0 + y1) / 2 - 4 + text(label) + font = savedFont.first + fontSize = savedFont.second + vertical = savedFont.third + color = savedColor + if (host.isNotEmpty()) regions.add(Region(x0, y0, x1, y1, host)) + } + + /** a tap on a region or button (the last defined first): its host text, ^ and a letter a control */ + override fun tap(fx: Double, fy: Double): ByteArray? { + val px = (fx * WIDTH).toInt() + val py = (fy * HEIGHT).toInt() + val r = regions.lastOrNull { px in it.x0..it.x1 && py in it.y0..it.y1 } ?: return null + val out = ArrayList() + var i = 0 + val s = r.send + while (i < s.length) { + val c = s[i] + if (c == '^' && i + 1 < s.length) { + out.add((s[i + 1].uppercaseChar().code and 0x1f).toByte()) + i += 2 + continue + } + out.add(Cp437.encode(c.code).toByte()) + i++ + } + return out.toByteArray() + } + + /** the picture put on the screen, after ANSI-BBS's */ + override fun flush() { + super.flush() + layer.flush() + } + + /** RIS: the picture, windows and regions as at the start, RIP on, the 80x43 asked for again */ + override fun reset() { + super.reset() + line = null + bang = false + escaped = false + swallowLf = false + held.setLength(0) + enabled = true + lineStart = true + resetWindows() + textScreen() + } + + /** a command's arguments: MegaNums (base 36) of a given width, then the text that's left */ + private class Args(val s: String, var i: Int) { + /** the next MegaNum, [width] digits (0 for what isn't there) */ + fun n(width: Int = 2): Int { + var v = 0 + repeat(width) { + val c = if (i < s.length) s[i] else '0' + i++ + v = v * 36 + when (c) { + in '0'..'9' -> c - '0' + in 'A'..'Z' -> c - 'A' + 10 + in 'a'..'z' -> c - 'a' + 10 + else -> 0 + } + } + return v + } + + /** the rest of the command, its text */ + fun rest(): String = if (i < s.length) s.substring(i) else "" + } + + companion object { + /** RIP's screen: the EGA's 640x350 */ + const val WIDTH = 640 + const val HEIGHT = 350 + /** an EGA pixel's height for its width, as BGI's circles allow for */ + private const val ASPECT = 0.775 + /** a RIP line's characters at most */ + private const val MAX_LINE = 1 shl 16 + + /** an EGA colour (0-63: rgbRGB, the lower case the 1/3 bits) as ARGB */ + fun ega(v: Int): Int { + /** one channel from its 2/3 bit [hi] and 1/3 bit [lo] */ + fun ch(hi: Int, lo: Int) = (if (v shr hi and 1 != 0) 0xaa else 0) + (if (v shr lo and 1 != 0) 0x55 else 0) + return (0xff shl 24) or (ch(2, 5) shl 16) or (ch(1, 4) shl 8) or ch(0, 3) + } + + /** the EGA's sixteen at the start */ + private val EGA = intArrayOf(0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63).map(::ega).toIntArray() + + /** BGI's fill patterns 2-11: lines, light slashes, slashes, backslashes, light backslashes, hatch, cross-hatch, interleave, wide dots, close dots */ + private val FILLS = listOf( + intArrayOf(0xff, 0xff, 0, 0, 0xff, 0xff, 0, 0), + intArrayOf(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80), + intArrayOf(0xe0, 0xc1, 0x83, 0x07, 0x0e, 0x1c, 0x38, 0x70), + intArrayOf(0xf0, 0x78, 0x3c, 0x1e, 0x0f, 0x87, 0xc3, 0xe1), + intArrayOf(0xa5, 0xd2, 0x69, 0xb4, 0x5a, 0x2d, 0x96, 0x4b), + intArrayOf(0xff, 0x88, 0x88, 0x88, 0xff, 0x88, 0x88, 0x88), + intArrayOf(0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81), + intArrayOf(0xcc, 0x33, 0xcc, 0x33, 0xcc, 0x33, 0xcc, 0x33), + intArrayOf(0x80, 0x00, 0x08, 0x00, 0x80, 0x00, 0x08, 0x00), + intArrayOf(0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00), + ) + } +} diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalBridge.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalBridge.kt index db8bd80..ec6a69a 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalBridge.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalBridge.kt @@ -65,6 +65,9 @@ class TerminalBridge(cols: Int, rows: Int) { /** a row (0: the screen's top) as runs */ fun runs(row: Int): List = emulator.runs(row, look) + /** a tap at ([fx], [fy]) (fractions of the screen from its top left) as the terminal takes it: the octets to send in Latin-1 (a RIP button's), "" for none */ + fun tap(fx: Double, fy: Double): String = emulator.tap(fx, fy)?.let(::latin1) ?: "" + /** the images seen in the rows asked for lately ([imageRuns]), by id, for [imagePixels] */ private val images = HashMap() diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalEmulator.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalEmulator.kt index 89cb561..94eb9d2 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalEmulator.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalEmulator.kt @@ -117,20 +117,20 @@ class TerminalEmulator( /** the colours as the view shows them (for OSC 4, 10, 11 and 12's questions): 0-255, 256 fg, 257 bg, 258 cursor */ var defaultColor: (Int) -> Int = { xtermColor(it) } - /** the remote's own colours (OSC 4, 10, 11, 12): ARGB, or -1 for the view's */ - private val colorOverrides = IntArray(259) { -1 } + /** the remote's own colours (OSC 4, 10, 11, 12) and the machines' palettes: opaque ARGB, or [NO_COLOR] for the view's */ + private val colorOverrides = IntArray(259) { NO_COLOR } - /** a colour the remote set (0-255, 256 fg, 257 bg, 258 cursor), or -1 */ - fun colorOverride(i: Int): Int = colorOverrides.getOrElse(i) { -1 } + /** a colour the remote set (0-255, 256 fg, 257 bg, 258 cursor), or [NO_COLOR] */ + fun colorOverride(i: Int): Int = colorOverrides.getOrElse(i) { NO_COLOR } - /** colour [i] (as [colorOverride] numbers them) set by the remote to [argb] */ + /** colour [i] (as [colorOverride] numbers them) set by the remote to [argb] (made opaque: [NO_COLOR] stays the only transparent one) */ internal fun setColorOverride(i: Int, argb: Int) { - if (i in colorOverrides.indices) colorOverrides[i] = argb + if (i in colorOverrides.indices) colorOverrides[i] = argb or (0xff shl 24) } /** the remote's colours in [range] back to the view's */ internal fun resetColors(range: IntRange) { - for (i in range) if (i in colorOverrides.indices) colorOverrides[i] = -1 + for (i in range) if (i in colorOverrides.indices) colorOverrides[i] = NO_COLOR } /** the columns the view should fit across its width (DECCOLM's 80 or 132, a Commodore's 40); 0: as many as the font gives */ @@ -244,6 +244,9 @@ class TerminalEmulator( private val sink = TermDecoder.Sink { cp -> interpreter.process(cp) } + /** a tap at ([fx], [fy]) (fractions of the screen from its top left) as the terminal takes it: what to send (a RIP button's text), or null if it takes none */ + fun tap(fx: Double, fy: Double): ByteArray? = interpreter.tap(fx, fy) + /** the remote's octets, decoded in [decoder]'s set (or as they are, to an interpreter that reads octets) */ fun append(data: ByteArray, length: Int = data.size) { if (interpreter.readsOctets) { @@ -861,7 +864,7 @@ class TerminalEmulator( vt52Keys = false sixelScrolling = true sixelCursorRight = false - colorOverrides.fill(-1) + colorOverrides.fill(NO_COLOR) link = null joinNext = false tabs = BooleanArray(cols) { it % 8 == 0 && it > 0 } @@ -957,6 +960,11 @@ class TerminalEmulator( } companion object { + /** + * [colorOverride]'s "none": transparent black, which no override is + * (not -1, which is opaque white) + */ + const val NO_COLOR = 0 /** the largest sixel image (pixels a side) */ const val MAX_IMAGE = 2048 diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalInterpreter.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalInterpreter.kt index 4eddcf6..b95c3d8 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalInterpreter.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalInterpreter.kt @@ -35,6 +35,9 @@ abstract class TerminalInterpreter(protected val t: TerminalEmulator) { /** what character [cp] typed or pasted sends on this terminal, as octets; null: [KeyEncoder]'s, in the character set */ open fun charOctets(cp: Int, alt: Boolean, ctrl: Boolean): ByteArray? = null + /** a tap at ([fx], [fy]), fractions of the screen's width and height from its top left: what it sends (a RIP button's text), or null if it's nothing to this terminal */ + open fun tap(fx: Double, fy: Double): ByteArray? = null + /** what came in a read has all been processed: a picture drawn meanwhile put on the screen */ open fun flush() {} diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalProfile.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalProfile.kt index 1f35dee..8380f2c 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalProfile.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalProfile.kt @@ -230,11 +230,42 @@ open class AnsiBbsProfile : XtermProfile() { override val charset: TermCharset? get() = TermCharset.CP437 /** a VGA's 8x16 code page 437 */ override val glyphs: BitmapFont? get() = LegacyCharsets.VGA + /** its machine's sixteen colours (ARGB, ANSI's order), over the theme's; null: the theme's */ + open val colors: IntArray? get() = null /** an [AnsiBbsInterpreter]: CTerm's additions */ override fun interpreter(t: TerminalEmulator): TerminalInterpreter = AnsiBbsInterpreter(t, this) } +/** + * An Amiga's BBS terminal (as NComm and Term are): ANSI in Latin-1, the + * Amiga's own set, drawn 8x8 as Topaz is (Unscii's glyphs), in colours + * from the Amiga's 12-bit palette + */ +open class AmigaAnsiProfile : AnsiBbsProfile() { + override val id get() = "amiga" + override val label get() = "Amiga ANSI" + override val term get() = "amiga" + // (BBSes find ANSI by it) + override val telnetType get() = "ANSI" + override val charset: TermCharset? get() = TermCharset.LATIN1 + /** 8x8, Topaz's size */ + override val glyphs: BitmapFont? get() = LegacyCharsets.GLYPHS + override val colors: IntArray? get() = AMIGA + + companion object { + /** ANSI's eight and their bright eight, as an Amiga's 4-bit levels */ + private val AMIGA = intArrayOf( + 0x000, 0xC22, 0x2C2, 0xCC2, 0x22C, 0xC2C, 0x2CC, 0xCCC, + 0x666, 0xF55, 0x5F5, 0xFF5, 0x55F, 0xF5F, 0x5FF, 0xFFF, + ).map { c -> + /** a 4-bit level as 0-255 */ + fun level(v: Int) = v * 17 + (0xff shl 24) or (level(c shr 8 and 15) shl 16) or (level(c shr 4 and 15) shl 8) or level(c and 15) + }.toIntArray() + } +} + /** The profiles there are, by id. */ object TerminalProfiles { val VT52: TerminalProfile = Vt52Profile() @@ -251,6 +282,9 @@ object TerminalProfiles { val VT525: TerminalProfile = Vt525Profile() val XTERM: TerminalProfile = XtermProfile() val ANSI_BBS: TerminalProfile = AnsiBbsProfile() + val AVATAR: TerminalProfile = AvatarProfile() + val AMIGA: TerminalProfile = AmigaAnsiProfile() + val RIP: TerminalProfile = RipProfile() val C64: TerminalProfile = C64Profile() val C128: TerminalProfile = C128Profile() val C128_80: TerminalProfile = C128VdcProfile() @@ -261,12 +295,14 @@ object TerminalProfiles { val ATARI: TerminalProfile = AtariProfile() val ATARI_INTL: TerminalProfile = AtariInternationalProfile() val TEK4014: TerminalProfile = Tek4014Profile() + val ATARI_ST: TerminalProfile = AtariStProfile() + val ATARI_ST_LOW: TerminalProfile = AtariStLowProfile() /** the default: what the app was before profiles */ val DEFAULT: TerminalProfile get() = XTERM /** in the pickers' order */ - val all: List = listOf(XTERM, ANSI_BBS, VT52, VT100, VT102, VT220, VT320, VT340, VT420, VT510, VT520, VT525, C64, C128, C128_80, PLUS4, VIC20, PET, PET80, ATARI, ATARI_INTL, VIEWDATA, MINITEL, TEK4014) + val all: List = listOf(XTERM, ANSI_BBS, RIP, AVATAR, AMIGA, VT52, VT100, VT102, VT220, VT320, VT340, VT420, VT510, VT520, VT525, C64, C128, C128_80, PLUS4, VIC20, PET, PET80, ATARI, ATARI_INTL, ATARI_ST, ATARI_ST_LOW, VIEWDATA, MINITEL, TEK4014) /** the profile [id] names, or null for none or one not known */ fun byId(id: String?): TerminalProfile? = all.firstOrNull { it.id == id } diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalRuns.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalRuns.kt index 7e42fb6..6bae993 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalRuns.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/TerminalRuns.kt @@ -48,7 +48,7 @@ fun TerminalEmulator.runs(row: Int, look: RunLook = RunLook()): List } if (i >= 0) { val o = colorOverride(i) - if (o != -1) return o + if (o != TerminalEmulator.NO_COLOR) return o } return look.theme.argb(c, isFg) } diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt420Interpreter.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt420Interpreter.kt index 79a5903..578421e 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt420Interpreter.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt420Interpreter.kt @@ -126,7 +126,7 @@ open class Vt420Interpreter(t: TerminalEmulator, profile: DecProfile) : VtInterp /** colour [n] of the 16 as the screen shows it now */ private fun color(n: Int): Int { val i = n.coerceIn(0, 15) - return t.colorOverride(i).takeIf { it != -1 } ?: t.defaultColor(i) + return t.colorOverride(i).takeIf { it != TerminalEmulator.NO_COLOR } ?: t.defaultColor(i) } /** DECCTR: the 16 colours, each Pc; 2 (RGB); its red, green and blue in percent */ diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt52Interpreter.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt52Interpreter.kt index 7262431..bb2a18a 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt52Interpreter.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/Vt52Interpreter.kt @@ -18,8 +18,10 @@ open class Vt52Interpreter( /** ESC <: back to ANSI mode (null: a real VT52, which has none) */ private val onAnsi: (() -> Unit)? = null, ) : TerminalInterpreter(t) { - private enum class State { GROUND, ESC, ROW, COL } + private enum class State { GROUND, ESC, ROW, COL, ARG } private var state = State.GROUND + /** an ESC command's argument character to come, for [escArgument] (an Atari ST's colours) */ + private var argFor = 0 private var graphics = false private var row = 0 private val printer = PrinterPort(t, listOf("\u001bX")) @@ -39,7 +41,7 @@ open class Vt52Interpreter( cp == 0x1b -> state = State.ESC cp < 0x20 -> control(cp) cp == 0x7f -> {} - else -> t.print(if (graphics) graphicsSet.map(cp) else cp) + else -> t.print(if (graphics) graphicsSet.map(cp) else charOf(cp)) } State.ESC -> { state = State.GROUND @@ -49,6 +51,10 @@ open class Vt52Interpreter( row = cp - 0x20 state = State.COL } + State.ARG -> { + state = State.GROUND + escArgument(argFor, cp) + } State.COL -> { state = State.GROUND // (a row off the screen leaves the cursor's; a column past the end is the last) @@ -58,6 +64,9 @@ open class Vt52Interpreter( } } + /** what character [cp] prints as (an Atari ST's own set maps its octets) */ + protected open fun charOf(cp: Int): Int = cp + /** a control: the bell, BS, HT, LF (auto printing), CR */ private fun control(cp: Int) { when (cp) { @@ -101,6 +110,15 @@ open class Vt52Interpreter( } } + /** ESC [command]'s argument to come: the next character goes to [escArgument] */ + protected fun takeArgument(command: Int) { + argFor = command + state = State.ARG + } + + /** the argument character [arg] of ESC [command] (one [takeArgument] asked for) */ + protected open fun escArgument(command: Int, arg: Int) {} + /** RIS: graphics off, no sequence under way, the printer port as at power-up */ override fun reset() { state = State.GROUND diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/VtInterpreter.kt b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/VtInterpreter.kt index 3a18f98..15887bd 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/terminal/VtInterpreter.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/terminal/VtInterpreter.kt @@ -713,7 +713,7 @@ open class VtInterpreter(t: TerminalEmulator, val profile: DecProfile) : Termina /** a colour set from its spec, or told (OSC's answer) when the spec is "?" */ private fun colorSpec(i: Int, spec: String, prefix: String) { if (spec == "?") { - val c = t.colorOverride(i).takeIf { it != -1 } ?: t.defaultColor(i) + val c = t.colorOverride(i).takeIf { it != TerminalEmulator.NO_COLOR } ?: t.defaultColor(i) /** a colour channel (0-255) as X's four hex digits */ fun h(v: Int) = (v * 0x101).toString(16).padStart(4, '0') val end = if (oscBel) "\u0007" else "\u001b\\" diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AmigaAnsiTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AmigaAnsiTest.kt new file mode 100644 index 0000000..37af0ab --- /dev/null +++ b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AmigaAnsiTest.kt @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.Test + +class AmigaAnsiTest { + + /** the Amiga: Latin-1, 8x8 glyphs for all of it, its colours over the theme's, the theme's back for the next profile */ + @Test + fun amiga() { + val p = TerminalProfiles.AMIGA as AmigaAnsiProfile + assertEquals(TermCharset.LATIN1, p.charset) + for (cp in 0xa1..0xff) assertTrue(LegacyCharsets.GLYPHS.has(cp), "U+" + cp.toString(16)) + val t = TerminalEmulator(80, 25, 100, p) {} + assertEquals(0xffcc2222.toInt(), t.colorOverride(1)) + assertEquals(0xffffffff.toInt(), t.colorOverride(15)) + t.profile = TerminalProfiles.ANSI_BBS + assertEquals(TerminalEmulator.NO_COLOR, t.colorOverride(1)) + } +} diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AtariStTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AtariStTest.kt new file mode 100644 index 0000000..5831b8e --- /dev/null +++ b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AtariStTest.kt @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue +import kotlin.test.Test + +class AtariStTest { + + private val replies = StringBuilder() + + /** a terminal of [profile] at its own size, whose replies are kept */ + private fun term(profile: TerminalProfile = TerminalProfiles.ATARI_ST): TerminalEmulator { + replies.setLength(0) + val m = profile as AtariStProfile + return TerminalEmulator(m.columns, m.rows, 100, profile) { b -> b.forEach { replies.append((it.toInt() and 0xff).toChar()) } } + } + + /** [s] fed in as Latin-1 octets */ + private fun TerminalEmulator.send(s: String) = append(s.latin1()) + + /** row [y]'s text, its trailing blanks gone */ + private fun TerminalEmulator.row(y: Int) = text(y, 0, y, cols - 1).trimEnd() + + /** the VT52's own: ESC Y to a place, ESC K to the line's end, ESC Z answered as a VT52 */ + @Test + fun vt52() { + val t = term() + t.send("\u001bY\"%hello\u001bD\u001bD\u001bK\u001bZ") + assertEquals(" hel", t.row(2)) + assertEquals("\u001b/K", replies.toString()) + } + + /** ESC b and ESC c: the pens (medium resolution's four: 3 black, 1 red); ESC p and q reverse */ + @Test + fun colours() { + val t = term() + t.send("A\u001bb1B\u001bc2\u001bb\u0003C\u001bpD\u001bqE") + val s = t.line(0).styles + assertEquals(3, Style.fg(s[0])) + assertEquals(1, Style.fg(s[1])) + assertEquals(3, Style.fg(s[2])) + assertEquals(2, Style.bg(s[2])) + assertTrue(Style.attrs(s[3]) and Style.INVERSE != 0) + assertEquals(0, Style.attrs(s[4]) and Style.INVERSE) + // (pen 5 on four colours is pen 1) + t.send("\u001bb5F") + assertEquals(1, Style.fg(t.line(0).styles[5])) + // low resolution has sixteen + val low = term(TerminalProfiles.ATARI_ST_LOW) + low.send("\u001bb\u000bX") + assertEquals(11, Style.fg(low.line(0).styles[0])) + assertEquals(40, low.cols) + } + + /** ESC E clears, ESC l erases the line, ESC o its start, ESC d the screen above, ESC L and M insert and delete lines */ + @Test + fun erases() { + val t = term() + t.send("one\r\ntwo\r\nthree\u001bo") + assertEquals("", t.row(2).trim()) + t.send("\u001bA\u001bl") + assertEquals("", t.row(1)) + assertEquals("one", t.row(0)) + t.send("\u001bH\u001bL") + assertEquals("", t.row(0)) + assertEquals("one", t.row(1)) + t.send("\u001bM") + assertEquals("one", t.row(0)) + t.send("\u001bE") + assertEquals("", t.row(0)) + assertEquals(0, t.cursorX) + } + + /** ESC j and k save and restore the place; ESC f and e hide and show the cursor; ESC w stops wrapping */ + @Test + fun cursorAndWrap() { + val t = term() + t.send("\u001bY%*\u001bj\u001bH\u001bk") + assertEquals(10, t.cursorX) + assertEquals(5, t.cursorY) + t.send("\u001bf") + assertFalse(t.cursorVisible) + t.send("\u001be") + assertTrue(t.cursorVisible) + t.send("\u001bH\u001bw" + "x".repeat(85)) + assertEquals("", t.row(1)) + } + + /** the ST's set above 0x7f: ß, Hebrew, Greek */ + @Test + fun characters() { + val t = term() + t.send("\u009eÂà") + assertEquals("ßאα", t.row(0)) + } + + /** the screen white (opaque white an override like any other colour, not taken for none) */ + @Test + fun whiteBackground() { + val t = term() + assertEquals(0xffffffff.toInt(), t.colorOverride(257)) + assertEquals(0xffffffff.toInt(), t.colorOverride(0)) + assertEquals(0xff000000.toInt(), t.colorOverride(256)) + } +} diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AvatarTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AvatarTest.kt new file mode 100644 index 0000000..ef30dfc --- /dev/null +++ b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/AvatarTest.kt @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.Test + +class AvatarTest { + + /** an 80x25 Avatar terminal in CP437 (the profile's, as the app sets it) */ + private fun term() = TerminalEmulator(80, 25, 100, TerminalProfiles.AVATAR) {}.also { it.decoder.charset = TermCharset.CP437 } + + /** [s] fed in as Latin-1 octets */ + private fun TerminalEmulator.send(s: String) = append(s.latin1()) + + /** row [y]'s text, its trailing blanks gone */ + private fun TerminalEmulator.row(y: Int) = text(y, 0, y, cols - 1).trimEnd() + + /** ^Y repeats a character; ^V ^H moves (row, column, from 1); ^V ^G clears to the line's end */ + @Test + fun repeatAndMove() { + val t = term() + t.send("\u0019-\u0005\u0016\u0008\u0002\u0003ab\u0016\u0005\u0016\u0007") + assertEquals("-----", t.row(0)) + assertEquals(" a", t.row(1)) + } + + /** ^V ^A: a PC attribute (bright yellow on blue: 0x1e), DLE-quoted; ^L clears to cyan (3) */ + @Test + fun attributes() { + val t = term() + t.send("\u0016\u0001\u001eX\u0016\u0001\u0010\u0094Y") + val s = t.line(0).styles + assertEquals(11, Style.fg(s[0])) + assertEquals(4, Style.bg(s[0])) + // (0x94: blink, red on blue) + assertEquals(1, Style.fg(s[1])) + assertTrue(Style.attrs(s[1]) and Style.BLINK != 0) + t.send("\u000cZ") + assertEquals("Z", t.row(0)) + assertEquals(6, Style.fg(t.line(0).styles[0])) + } + + /** AVT/0+: ^V ^J scrolls an area up, ^V ^Y repeats a pattern, ^V ^N deletes a character, ^V ^I inserts */ + @Test + fun avt0Plus() { + val t = term() + t.send("aaaa\r\nbbbb\r\ncccc") + // the area rows 1-3, columns 2-3, up one + t.send("\u0016\u000a\u0001\u0001\u0002\u0003\u0003") + assertEquals("abba", t.row(0)) + assertEquals("bccb", t.row(1)) + assertEquals("c c", t.row(2)) + t.send("\u0016\u0008\u0005\u0001\u0016\u0019\u0002xy\u0003") + assertEquals("xyxyxy", t.row(4)) + t.send("\u0016\u0008\u0005\u0001\u0016\u000e") + assertEquals("yxyxy", t.row(4)) + t.send("\u0016\u0009Q") + assertEquals("Qyxyxy", t.row(4)) + } + + /** ANSI still works underneath */ + @Test + fun ansiToo() { + val t = term() + t.send("\u001b[3;5Hhi") + assertEquals(" hi", t.row(2)) + } +} diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/terminal/PetsciiTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/PetsciiTest.kt index 66621bc..e94bc88 100644 --- a/shared/src/commonTest/kotlin/org/dmodem/app/terminal/PetsciiTest.kt +++ b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/PetsciiTest.kt @@ -39,7 +39,7 @@ class PetsciiTest { t.profile = TerminalProfiles.XTERM assertEquals(0, t.requestedColumns) assertEquals(0, t.requestedRows) - assertEquals(-1, t.colorOverride(2)) + assertEquals(TerminalEmulator.NO_COLOR, t.colorOverride(2)) assertNull(t.glyphs) assertEquals(22, term(TerminalProfiles.VIC20).requestedColumns) assertEquals(80, term(TerminalProfiles.C128_80).requestedColumns) diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/terminal/RipTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/RipTest.kt new file mode 100644 index 0000000..594fc1e --- /dev/null +++ b/shared/src/commonTest/kotlin/org/dmodem/app/terminal/RipTest.kt @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Copyright (C) 2026 Neo-Desktop +package org.dmodem.app.terminal + +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.test.Test + +class RipTest { + + private val replies = StringBuilder() + + /** a RIP terminal at its 80x43 (10x20 cells: an 800x860 picture), whose replies are kept */ + private fun term(): TerminalEmulator { + replies.setLength(0) + return TerminalEmulator(80, 43, 100, TerminalProfiles.RIP) { b -> b.forEach { replies.append((it.toInt() and 0xff).toChar()) } } + .also { it.decoder.charset = TermCharset.CP437 } + } + + /** [s] fed in as Latin-1 octets */ + private fun TerminalEmulator.send(s: String) = append(s.latin1()) + + /** [v] as a MegaNum of [w] digits */ + private fun m(v: Int, w: Int = 2): String { + val d = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + var n = v + val b = CharArray(w) + for (i in w - 1 downTo 0) { b[i] = d[n % 36]; n /= 36 } + return b.concatToString() + } + + /** whether RIP's pixel ([x], [y]) (of 640x350) is drawn: the picture's pixel there, through its tile */ + private fun TerminalEmulator.drawn(x: Int, y: Int): Int { + val px = x * 800 / 640 + val py = y * 860 / 350 + val tile = line(py / 20).extra(px / 10)?.image?.takeIf { it.image.picture } ?: return 0 + return tile.image.pixels[py * tile.image.width + px] + } + + /** row [y]'s text, its trailing blanks gone */ + private fun TerminalEmulator.row(y: Int) = text(y, 0, y, cols - 1).trimEnd() + + /** ESC [ ! asks for RIP: its version; other CSIs still ANSI's */ + @Test + fun query() { + val t = term() + t.send("\u001b[!") + assertEquals("RIPSCRIP015400", replies.toString()) + t.send("\u001b[1mB\u001b[0m") + assertTrue(Style.attrs(t.line(0).styles[0]) and Style.BOLD != 0) + assertEquals("B", t.row(0)) + } + + /** a line in a colour; a ! that doesn't start a RIP line is text */ + @Test + fun lineAndText() { + val t = term() + t.send("!|c0E|L${m(10)}${m(100)}${m(300)}${m(100)}\r\nHello!\r\n!not rip") + assertEquals(0xffffff55.toInt(), t.drawn(150, 100)) + assertEquals(0, t.drawn(150, 200)) + assertEquals("Hello!", t.row(0)) + assertEquals("!not rip", t.row(1)) + } + + /** B: a bar in the fill colour; R and F: an outline flood-filled inside */ + @Test + fun fills() { + val t = term() + t.send("!|S0104|B${m(10)}${m(10)}${m(50)}${m(50)}\r\n") + assertEquals(RipInterpreter.ega(4), t.drawn(30, 30)) + t.send("!|c0F|R${m(100)}${m(100)}${m(200)}${m(200)}|S0102|F${m(150)}${m(150)}0F\r\n") + assertEquals(RipInterpreter.ega(2), t.drawn(150, 150)) + assertEquals(0, t.drawn(250, 150)) + } + + /** a button: drawn, and a tap on it sends its host text (^M a CR, \| a bar), a tap elsewhere nothing */ + @Test + fun buttons() { + val t = term() + t.send("!|1U${m(64)}${m(35)}${m(192)}${m(70)}0000<>Go<>G\\|o^M\r\n") + assertTrue(t.drawn(100, 50) != 0) + assertContentEquals("G|o\r".latin1(), t.tap(0.2, 0.15)) + assertNull(t.tap(0.9, 0.9)) + // 1K: the regions gone + t.send("!|1K\r\n") + assertNull(t.tap(0.2, 0.15)) + } + + /** a line continued with a backslash at its end; * resets the windows (the picture gone) */ + @Test + fun continuationAndReset() { + val t = term() + t.send("!|c0F|L${m(10)}${m(10)}\\\r\n${m(300)}${m(10)}\r\n") + assertTrue(t.drawn(150, 10) != 0) + t.send("!|*\r\n") + assertEquals(0, t.drawn(150, 10)) + } + + /** ESC [ 1 ! turns RIP off: !| is text */ + @Test + fun disabled() { + val t = term() + t.send("\u001b[1!!|c0F\r\n") + assertEquals("!|c0F", t.row(0)) + } +} diff --git a/shared/src/jvmTest/kotlin/org/dmodem/app/terminal/XtermFeaturesTest.kt b/shared/src/jvmTest/kotlin/org/dmodem/app/terminal/XtermFeaturesTest.kt index 9989eb5..3e1eb44 100644 --- a/shared/src/jvmTest/kotlin/org/dmodem/app/terminal/XtermFeaturesTest.kt +++ b/shared/src/jvmTest/kotlin/org/dmodem/app/terminal/XtermFeaturesTest.kt @@ -139,8 +139,8 @@ class XtermFeaturesTest { assertEquals(0xffffeedd.toInt(), t.colorOverride(256)) assertEquals(0xff000000.toInt(), t.colorOverride(257)) t.send("\u001b]104\u0007\u001b]110\u0007") - assertEquals(-1, t.colorOverride(1)) - assertEquals(-1, t.colorOverride(256)) + assertEquals(TerminalEmulator.NO_COLOR, t.colorOverride(1)) + assertEquals(TerminalEmulator.NO_COLOR, t.colorOverride(256)) assertEquals(0xff000000.toInt(), t.colorOverride(257)) assertNull(TerminalEmulator.parseColor("red")) } diff --git a/tools/gen_legacy_charsets.py b/tools/gen_legacy_charsets.py index e985948..14e7e74 100644 --- a/tools/gen_legacy_charsets.py +++ b/tools/gen_legacy_charsets.py @@ -132,6 +132,8 @@ def main(): t = [m.get(c, 0) for c in range(256)] tables.append((name, f, what, t)) used.update(cp for cp in t if cp) + # (Latin-1's letters and signs: an Amiga's set, drawn as Topaz is, 8x8) + used.update(range(0xA1, 0x100)) missing = sorted(cp for cp in used if cp not in font) if missing: print("no glyph for: " + " ".join("U+%04X" % cp for cp in missing), file=sys.stderr) @@ -146,7 +148,7 @@ def main(): w(" * The 8-bit home computers' character sets, each code (0x00..0xff) as its") w(" * Unicode code point (0: not a character), from Rebecca Bettencourt's") w(" * Legacy Computing mappings; and [GLYPHS], an 8x8 glyph for every one of") - w(" * them, and [VGA], code page 437 in 8x16 as a PC's VGA showed it; both") + w(" * them (and Latin-1's, an Amiga's), and [VGA], code page 437 in 8x16 as a PC's VGA showed it; both") w(" * from Unscii (public domain; the few it hasn't drawn in the generator).") w(" */") w("internal object LegacyCharsets {")