diff --git a/package-lock.json b/package-lock.json index b7ad1120ee..2b0f3ac41d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5405,17 +5405,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/object-keys": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", - "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", - "dev": true, - "license": "MIT", - "optional": true, - "engines": { - "node": ">= 0.4" - } - }, "node_modules/obscenity": { "version": "0.4.5", "resolved": "https://registry.npmjs.org/obscenity/-/obscenity-0.4.5.tgz", -- knotmirror 0.0.1 From b9916048ff77a552aac272e331f3312290eea1f4 Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Sun, 28 Sep 2025 00:36:56 +0000 Subject: [PATCH 2/7] settings update --- .vscode/settings.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e26d021f4a..9e292b93ec 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,6 @@ }, "workbench.list.smoothScrolling": true, "terminal.integrated.smoothScrolling": true, - "notebook.lineNumbers": "off" + "notebook.lineNumbers": "off", + "editor.minimap.autohide": "mouseover" } \ No newline at end of file -- knotmirror 0.0.1 From e166ca033d276c2ae4df762640709eb10b7adaad Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Sun, 28 Sep 2025 05:04:05 +0000 Subject: [PATCH 3/7] work on prompt hud corner label syntax --- plans/kidlisp-hud-text-layout-issue.md | 139 ++ reference/tools/recording/headless.mjs | 162 ++- system/public/aesthetic.computer/bios.mjs | 8 +- .../aesthetic.computer/disks/common/fonts.mjs | 99 +- .../disks/debug-kidlisp-hud.mjs | 107 ++ system/public/aesthetic.computer/lib/disk.mjs | 1227 +++++++++++------ .../public/aesthetic.computer/lib/graph.mjs | 87 +- .../public/aesthetic.computer/lib/kidlisp.mjs | 441 ++++-- system/public/aesthetic.computer/lib/type.mjs | 198 ++- 9 files changed, 1844 insertions(+), 624 deletions(-) create mode 100644 plans/kidlisp-hud-text-layout-issue.md create mode 100644 system/public/aesthetic.computer/disks/debug-kidlisp-hud.mjs diff --git a/plans/kidlisp-hud-text-layout-issue.md b/plans/kidlisp-hud-text-layout-issue.md new file mode 100644 index 0000000000..6b5ad1e4bc --- /dev/null +++ b/plans/kidlisp-hud-text-layout-issue.md @@ -0,0 +1,139 @@ +# KidLisp HUD Text Layout Issue + +## Problem Summary +KidLisp syntax-highlighted text in corner HUD labels is being forced into a single column on the left side of the screen, instead of maintaining the original multi-column code layout with proper character positioning. + +## Current Status +- ✅ **QR corner label visible** - Fixed by reverting to default font (font_1) +- ✅ **QR size optimized** - Set to 1:1 pixel size +- ✅ **Parentheses flashing fixed** - Excluded from timing-based color changes +- ✅ **Shadow conflicts resolved** - Simplified to uniform black shadows +- ❌ **Text layout broken** - All characters forced to left side in single column + +## Expected Behavior +Text should render in natural multi-column layout matching original KidLisp code structure: +``` +fade:red-blue-black-blue-red +ink (? rainbow white 0) (1s... 24 64) +line w/2 0 w/2 h +(spin (2s... -1.125 1.125)) (zoom 1.1) +(0.5s (contrast 1.05)) +(scroll (? -0.1 0 0.1) (? -0.1 0 0.1)) +ink (? cyan yellow magenta) 8 +circle w/2 h/2 (? 2 4 8) +``` + +Characters should be positioned at 6px intervals across the natural width (~228px), with syntax highlighting intact. + +## Actual Behavior +All characters appear in a narrow single column on the left side of the screen, regardless of: +- Buffer width (set to natural 228px) +- Bounds constraint (`{ w: bufferW, h: bufferH }`) +- Word wrap disabled (`false`) +- Clean text vs color-coded text + +## Technical Investigation Done + +### Root Cause Analysis +1. **Text Content**: Confirmed not the issue - tested with both: + - Clean KidLisp source (28-38 chars per line, fits in 228px buffer) + - Original color-coded text (157-186 chars per line with embedded color codes) + +2. **Buffer Sizing**: Confirmed working correctly: + - Natural width: 228px (calculated from text.box()) + - Screen width: 253px (no clipping needed) + - Buffer created at full natural width + +3. **Text Rendering Parameters**: All tested: + - Bounds constraint: `{ w: 228, h: 88 }` + - Word wrap: `false` + - Position: `{ x: 0, y: 0 }` + - Font: Default (font_1, 6px×10px) + +### Test Results +Created isolated test (`test-text-wrapping.mjs`) showing: +- Line 0: 28 chars = 168px (✅ fits in 228px buffer, should spread 0-162px) +- Line 1: 39 chars = 234px (❌ slightly too long, would wrap to 2 lines) +- Lines 2-7: 14-22 chars = 84-132px (✅ all fit, should spread naturally) + +### Code Locations +- **Main rendering**: `/workspaces/aesthetic-computer/system/public/aesthetic.computer/lib/disk.mjs` lines ~9570-9580 +- **Buffer creation**: Lines ~9370-9380 +- **Text processing**: Lines ~9450-9460 + +## Current Implementation +```javascript +// Use full buffer bounds to preserve character positioning +const boundsConstraint = { w: bufferW, h: bufferH }; + +// Go back to the original approach - render the color-coded text as intended +// The issue is not the text content but how we handle the color system + +// Render text with color codes processed by the system +$.write( + text, + { x: 0 + currentHUDScrub, y: 0 }, + boundsConstraint, // Use full buffer bounds to allow natural flow + 1, // scale + false, // No word wrap - preserve original layout + useTinyHudLabel ? "MatrixChunky8" : undefined +); +``` + +## Suspected Issue +The `$.write()` function appears to have internal text wrapping/positioning logic that overrides our bounds and word wrap settings when processing color-coded text. Even with: +- `bounds: { w: 228, h: 88 }` +- `wordWrap: false` +- Natural buffer width of 228px + +Characters are still forced into a narrow column on the left. + +## Next Steps for Investigation + +### 1. Deep Dive into $.write() Function +- Locate implementation of `$.write()` in the codebase +- Understand how it processes color codes (e.g., `\\cyan\\`, `\\red\\`) +- Check if there's internal width limiting or column forcing logic +- Test if color code processing is causing the positioning issue + +### 2. Alternative Rendering Approaches +- **Character-by-character rendering**: Manually position each character +- **Line-by-line with color processing**: Process color codes then render each line +- **Direct pixel manipulation**: Bypass text system entirely for positioning + +### 3. Font System Investigation +- Check if default font (font_1) has positioning limitations +- Test with different fonts to isolate font-specific issues +- Verify font metrics are being calculated correctly (6px per character) + +### 4. Color Code Processing +- Investigate how color codes like `\\mediumseagreen\\fade\\lime\\` are parsed +- Check if the parsing process is affecting character positioning +- Test rendering plain text (without color codes) vs color-coded text + +### 5. Buffer vs Screen Rendering +- Verify buffer is being created at correct dimensions (228×88px) +- Check how buffer is composited to screen in main thread +- Ensure buffer-to-screen positioning preserves character layout + +## Environment Details +- Screen width: 253px (varies, was 195px earlier) +- Font: Default (font_1) - 6px×10px glyphs, 144×66 buffer +- Buffer: 228×88px (natural text dimensions) +- Characters should position at: 0px, 6px, 12px, 18px, 24px, etc. + +## Test Commands +```bash +# Get actual KidLisp source +st roz + +# Run text wrapping analysis +node test-text-wrapping.mjs +``` + +## Files Modified +- `/workspaces/aesthetic-computer/system/public/aesthetic.computer/lib/disk.mjs` +- `/workspaces/aesthetic-computer/test-text-wrapping.mjs` (test file) + +## Priority +**HIGH** - This affects the core functionality of KidLisp piece display and user experience. The QR functionality works, but without proper text layout, the syntax highlighting and code readability are severely compromised. \ No newline at end of file diff --git a/reference/tools/recording/headless.mjs b/reference/tools/recording/headless.mjs index 34a646af56..0240b773bf 100644 --- a/reference/tools/recording/headless.mjs +++ b/reference/tools/recording/headless.mjs @@ -165,10 +165,21 @@ export class HeadlessAC { // Create a new KidLisp instance for the embedded layer kidlispInstance = new this.KidLisp(); - // Restore minimal state for embedded layer instance + // Restore complete state for embedded layer instance including timing if (layerMeta.kidlispInstanceState) { kidlispInstance.frameCount = layerMeta.kidlispInstanceState.frameCount || 0; kidlispInstance.localFrameCount = layerMeta.kidlispInstanceState.localFrameCount || 0; + + // CRITICAL: Restore timing state for persistent animations + kidlispInstance.lastSecondExecutions = layerMeta.kidlispInstanceState.lastSecondExecutions || []; + kidlispInstance.sequenceCounters = new Map( + Object.entries(layerMeta.kidlispInstanceState.sequenceCounters || {}) + ); + kidlispInstance.timingStates = new Map( + Object.entries(layerMeta.kidlispInstanceState.timingStates || {}) + ); + + console.log(`🕐 HEADLESS DEBUG: Restored timing state for ${layerMeta.cacheId}: ${kidlispInstance.lastSecondExecutions.length} executions, ${kidlispInstance.sequenceCounters.size} counters, ${kidlispInstance.timingStates.size} states`); } // Set the source code for the layer @@ -386,17 +397,48 @@ export class HeadlessAC { height: layer.buffer.height, filename: this.saveEmbeddedLayerBuffer(layer.cacheId || layer.id || `layer_${Date.now()}`, layer.buffer.pixels) } : null, - // Save minimal KidLisp instance state - mainly for frame counting + // Save complete KidLisp instance state including timing for persistence kidlispInstanceState: layer.kidlispInstance ? { frameCount: layer.kidlispInstance.frameCount || 0, - localFrameCount: layer.localFrameCount || 0 + localFrameCount: layer.localFrameCount || 0, + // CRITICAL: Save timing state for persistent animations + lastSecondExecutions: layer.kidlispInstance.lastSecondExecutions || [], + sequenceCounters: layer.kidlispInstance.sequenceCounters ? + Object.fromEntries(layer.kidlispInstance.sequenceCounters) : {}, + timingStates: layer.kidlispInstance.timingStates ? + Object.fromEntries(layer.kidlispInstance.timingStates) : {} } : null })) : [], - // Save embedded layer cache structure with full layer references - embeddedLayerCache: this.kidlispInstance.embeddedLayerCache ? Object.fromEntries(this.kidlispInstance.embeddedLayerCache) : {}, + // Save embedded layer cache structure WITHOUT pixel buffer data + embeddedLayerCache: this.kidlispInstance.embeddedLayerCache ? + Object.fromEntries( + Array.from(this.kidlispInstance.embeddedLayerCache.entries()).map(([key, layer]) => [ + key, + { + ...layer, + // Remove the buffer pixels data, keep only metadata + buffer: layer.buffer ? { + width: layer.buffer.width, + height: layer.buffer.height, + filename: layer.buffer.filename || `${key}.bin` + } : null, + // Clean kidlispInstance state to avoid circular refs but preserve timing + kidlispInstance: layer.kidlispInstance ? { + frameCount: layer.kidlispInstance.frameCount || 0, + localFrameCount: layer.kidlispInstance.localFrameCount || 0, + // CRITICAL: Save timing state for persistent animations + lastSecondExecutions: layer.kidlispInstance.lastSecondExecutions || [], + sequenceCounters: layer.kidlispInstance.sequenceCounters ? + Object.fromEntries(layer.kidlispInstance.sequenceCounters) : {}, + timingStates: layer.kidlispInstance.timingStates ? + Object.fromEntries(layer.kidlispInstance.timingStates) : {} + } : null + } + ]) + ) : {}, - // Local environment - localEnv: this.kidlispInstance.localEnv || {}, + // Local environment (cleaned of any buffer references) + localEnv: this.cleanEnvForSerialization(this.kidlispInstance.localEnv || {}), // Rainbow and zebra color cycling state rainbowState: getRainbowState(), @@ -406,6 +448,29 @@ export class HeadlessAC { return {}; } + // Clean environment objects of pixel buffer data for serialization + cleanEnvForSerialization(env) { + if (!env || typeof env !== 'object') return env; + + const cleaned = {}; + for (const [key, value] of Object.entries(env)) { + if (key === 'screen' && value && value.pixels) { + // Clean screen buffer - keep metadata but remove pixel data + cleaned[key] = { + width: value.width, + height: value.height, + // Don't serialize the pixel array + }; + } else if (value && typeof value === 'object' && !Array.isArray(value)) { + // Recursively clean nested objects + cleaned[key] = this.cleanEnvForSerialization(value); + } else { + cleaned[key] = value; + } + } + return cleaned; + } + // Enable V8 optimizations for performance enableV8Optimizations() { // Hint to V8 that these are hot functions @@ -873,7 +938,50 @@ export class HeadlessAC { lerp: (start, stop, amount) => start + (stop - start) * amount, clamp: (value, min, max) => Math.max(min, Math.min(max, value)), dist: (x1, y1, x2, y2) => Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2), - timestamp: () => api.simulationTime || Date.now() // Use deterministic time if available + timestamp: () => api.simulationTime || Date.now(), // Use deterministic time if available + + // Rainbow color cycling for pieces like $bair + rainbow: (offset = 0) => { + // Define rainbow colors (ROYGBIV) + const rainbowColors = [ + [255, 0, 0], // red + [255, 165, 0], // orange + [255, 255, 0], // yellow + [0, 128, 0], // green + [0, 0, 255], // blue + [75, 0, 130], // indigo + [238, 130, 238], // violet + ]; + + // Initialize rainbow state if not exists + if (self.rainbowCallCount === undefined) { + self.rainbowCallCount = 0; + self.rainbowFrameAdvanced = false; + self.currentRainbowIndex = 0; + self.lastRainbowFrame = -1; + } + + const currentFrame = self.frameCount || 0; + + // Reset rainbow state at the start of each new frame + if (currentFrame !== self.lastRainbowFrame) { + self.rainbowFrameAdvanced = false; + self.rainbowCallCount = 0; + self.lastRainbowFrame = currentFrame; + } + + // Advance the rainbow index once per frame on the first call + if (!self.rainbowFrameAdvanced) { + self.currentRainbowIndex = (self.currentRainbowIndex + 1) % rainbowColors.length; + self.rainbowFrameAdvanced = true; + } + + // Each call within the frame gets the current color + offset + call number + const finalIndex = (self.currentRainbowIndex + offset + self.rainbowCallCount) % rainbowColors.length; + self.rainbowCallCount++; // Increment for next call in same frame + + return rainbowColors[finalIndex].slice(); // Return a copy + } }; // Add clock utilities for timing-based animations @@ -898,6 +1006,20 @@ export class HeadlessAC { // Basic flood fill implementation if (self.graph && self.graph.flood) { console.log(`🔧 FLOOD DEBUG: Using graph.flood, api.screen sample before:`, Array.from(api.screen.pixels.slice(0, 20))); + console.log(`🎨 FLOOD DEBUG: Current drawing color 'c':`, self.graph.c || 'undefined'); + + // If we're in random color mode (after first-line color wipe), use random colors for flood + if (self.useRandomColorsForDrawing) { + const randomColor = [ + Math.floor(Math.random() * 256), + Math.floor(Math.random() * 256), + Math.floor(Math.random() * 256), + 255 + ]; + console.log(`🎲 FLOOD: Using random color in random drawing mode:`, randomColor); + self.graph.color(...randomColor); + } + self.graph.flood(...args); console.log(`🔧 FLOOD DEBUG: Using graph.flood, api.screen sample after:`, Array.from(api.screen.pixels.slice(0, 20))); } else { @@ -950,7 +1072,20 @@ export class HeadlessAC { // Random choice function - KidLisp uses (?) for random values api['?'] = function(...choices) { logCall('?', choices); - if (choices.length === 0) return 0; + + // If no choices provided, return a random color name (like original AC behavior) + if (choices.length === 0) { + // Define CSS color names (subset of the main ones for random selection) + const cssColorNames = [ + 'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', + 'pink', 'cyan', 'magenta', 'lime', 'purple', 'brown', 'black', 'white', + 'gray', 'salmon', 'coral', 'gold', 'silver', 'navy', 'teal', 'olive' + ]; + const randomColor = cssColorNames[Math.floor(Math.random() * cssColorNames.length)]; + console.log(`🎲 Random color (no args): ${randomColor}`); + return randomColor; + } + const randomIndex = Math.floor(Math.random() * choices.length); const result = choices[randomIndex]; console.log(`🎲 Random choice from [${choices.join(', ')}]: ${result}`); @@ -1230,10 +1365,8 @@ export class HeadlessAC { // Patch the KidLisp instance to use simulation time self.kidlispInstance.originalDateNow = originalDateNow; - // Reset timing state since we're switching to simulation time - self.kidlispInstance.lastSecondExecutions = {}; - self.kidlispInstance.sequenceCounters = new Map(); - console.log('🔄 Reset KidLisp timing state for simulation mode'); + // Note: Timing state preservation is now handled by state restoration + console.log('🔄 Pre-set KidLisp state before API creation'); // Restore KidLisp state if available - use the enhanced state restoration if (self.kidlispState) { @@ -1288,6 +1421,9 @@ export class HeadlessAC { if (self.kidlispInstance.firstLineColor && (api.frameIndex === undefined || api.frameIndex === 0)) { console.log(`🎨 Detected first-line color: ${self.kidlispInstance.firstLineColor} (applying on frame 0 only)`); api.wipe(self.kidlispInstance.firstLineColor); + // Mark that we should use random colors for drawing operations after first-line color wipe + self.useRandomColorsForDrawing = true; + console.log(`🎨 Enabled random color mode for drawing operations`); } else if (self.kidlispInstance.firstLineColor) { console.log(`🎨 First-line color ${self.kidlispInstance.firstLineColor} detected but skipping wipe (frame ${api.frameIndex})`); } diff --git a/system/public/aesthetic.computer/bios.mjs b/system/public/aesthetic.computer/bios.mjs index 29257f0fc5..be502ef094 100644 --- a/system/public/aesthetic.computer/bios.mjs +++ b/system/public/aesthetic.computer/bios.mjs @@ -11086,7 +11086,7 @@ async function boot(parsed, bpm = 60, resolution, debug) { }); } - // Add debug logging for qrFullscreenLabel + // Add debug logging for qrFullscreenLabel and qrOverlay if (name === "qrFullscreenLabel") { console.log(`🔍 Creating ImageData for qrFullscreenLabel:`, { pixelsType: typeof o.img.pixels, @@ -11097,6 +11097,8 @@ async function boot(parsed, bpm = 60, resolution, debug) { }); } + + // Use graphics optimizer if available, fallback to traditional method if (window.pixelOptimizer) { imageData = window.pixelOptimizer.createImageDataZeroCopy( @@ -11374,10 +11376,6 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (!skipImmediateOverlays && paintOverlays["qrOverlay"]) { paintOverlays["qrOverlay"](); - } else if (skipImmediateOverlays) { - // console.log("🔲 Skipping immediate QR overlay painting (async mode)"); - } else { - // QR overlay painter not found (no logging) } // Paint hitbox debug overlay immediately if debug is enabled diff --git a/system/public/aesthetic.computer/disks/common/fonts.mjs b/system/public/aesthetic.computer/disks/common/fonts.mjs index 93ea314d89..a09775612d 100644 --- a/system/public/aesthetic.computer/disks/common/fonts.mjs +++ b/system/public/aesthetic.computer/disks/common/fonts.mjs @@ -120,10 +120,101 @@ export const MatrixChunky8 = { proportional: true, // Flag indicating this is a proportional font // Character advance widths for proportional spacing advances: { - 'A': 4, 'B': 4, 'C': 4, 'D': 4, 'E': 4, 'F': 4, 'G': 4, 'H': 5, 'I': 4, 'J': 4, 'K': 4, 'L': 4, 'M': 6, 'N': 5, 'O': 5, 'P': 4, 'Q': 4, 'R': 4, 'S': 4, 'T': 4, 'U': 4, 'V': 4, 'W': 6, 'X': 4, 'Y': 4, 'Z': 4, - 'a': 4, 'b': 4, 'c': 4, 'd': 4, 'e': 4, 'f': 4, 'g': 4, 'h': 4, 'i': 4, 'j': 4, 'k': 4, 'l': 4, 'm': 6, 'n': 4, 'o': 4, 'p': 4, 'q': 4, 'r': 4, 's': 4, 't': 4, 'u': 4, 'v': 4, 'w': 6, 'x': 4, 'y': 4, 'z': 4, - '0': 4, '1': 4, '2': 4, '3': 4, '4': 4, '5': 4, '6': 4, '7': 4, '8': 4, '9': 4, - ' ': 2, '.': 2, ',': 2, '!': 2, '?': 4, ':': 2, ';': 2, '-': 4, '+': 4, '=': 4, '<': 4, '>': 4, '/': 4, '\\': 4, '|': 2, '"': 4, "'": 2, '(': 4, ')': 4, '[': 4, ']': 2, '{': 2, '}': 2, '@': 5, '#': 5, '$': 4, '%': 5, '^': 4, '&': 5, '*': 4, '_': 4, '~': 5 + ' ': 2, + '!': 2, + '"': 4, + '#': 8, + '$': 4, + '%': 4, + '&': 5, + '\'': 2, + '(': 7, + ')': 4, + '*': 5, + '+': 4, + ',': 2, + '-': 4, + '.': 2, + '/': 4, + '0': 4, + '1': 4, + '2': 4, + '3': 4, + '4': 4, + '5': 4, + '6': 4, + '7': 4, + '8': 4, + '9': 4, + ':': 2, + ';': 2, + '<': 4, + '=': 4, + '>': 4, + '?': 4, + '@': 5, + 'A': 4, + 'B': 4, + 'C': 4, + 'D': 4, + 'E': 4, + 'F': 4, + 'G': 5, + 'H': 5, + 'I': 4, + 'J': 4, + 'K': 4, + 'L': 4, + 'M': 6, + 'N': 5, + 'O': 5, + 'P': 4, + 'Q': 5, + 'R': 4, + 'S': 4, + 'T': 4, + 'U': 4, + 'V': 4, + 'W': 6, + 'X': 4, + 'Y': 4, + 'Z': 4, + '[': 4, + "\\": 4, + ']': 4, + '^': 4, + '_': 4, + '`': 3, + 'a': 4, + 'b': 4, + 'c': 4, + 'd': 4, + 'e': 4, + 'f': 4, + 'g': 4, + 'h': 4, + 'i': 4, + 'j': 4, + 'k': 4, + 'l': 4, + 'm': 6, + 'n': 4, + 'o': 4, + 'p': 4, + 'q': 4, + 'r': 4, + 's': 4, + 't': 4, + 'u': 4, + 'v': 4, + 'w': 6, + 'x': 4, + 'y': 4, + 'z': 4, + '{': 4, + '|': 2, + '}': 4, + '~': 4 }, // BDF overrides for character positioning adjustments bdfOverrides: { diff --git a/system/public/aesthetic.computer/disks/debug-kidlisp-hud.mjs b/system/public/aesthetic.computer/disks/debug-kidlisp-hud.mjs new file mode 100644 index 0000000000..cc45244695 --- /dev/null +++ b/system/public/aesthetic.computer/disks/debug-kidlisp-hud.mjs @@ -0,0 +1,107 @@ +// Text Layout Debug Test Piece +// Tests the exact KidLisp HUD rendering scenario to identify the single column issue + +const { floor } = Math; + +// Sample KidLisp-style text with color codes (simplified for testing) +const COLOR_CODED_TEXT = `\\cyan\\fade\\lime\\:\\blue\\red-blue-black-blue-red\\reset\\ +\\yellow\\ink\\reset\\ \\orange\\test\\reset\\ \\pink\\123\\reset\\`; + +// Text that contains timing expressions that cause blinking +const TIMING_TEXT = `\\cyan\\fade\\lime\\:\\blue\\red-blue-black-blue-red\\reset\\ +\\yellow\\(\\reset\\\\orange\\1s...\\reset\\ \\pink\\test\\reset\\ \\lime\\123\\reset\\\\yellow\\)\\reset\\`; + +const CLEAN_TEXT = `fade:red-blue-black-blue-red +ink test 123`; + +let testMode = 0; // 0: clean text, 1: color-coded text, 2: buffer test +let frameCount = 0; + +function boot({ help }) { + help.choose(["clean", "colors", "buffer"]); +} + +function paint({ wipe, ink, write, screen, paste, painting, help }) { + wipe(20, 20, 40); // Dark blue background + + frameCount++; + + // Test title + ink("white").write("KidLisp HUD Text Layout Debug", 10, 10); + ink("yellow").write(`Mode: ${["Clean Text", "Color-Coded Text", "Buffer Test"][testMode]}`, 10, 30); + ink("gray").write("Press numbers 1-3 to switch modes", 10, 50); + + const startY = 80; + + if (testMode === 0) { + // Test 1: Clean text (should work correctly) + ink("lime").write("TEST 1: Clean text (expected to work):", 10, startY); + + // Test different rendering approaches + ink("white").write(CLEAN_TEXT, 10, startY + 20); + + ink("cyan").write("Same text with bounds:", 10, startY + 60); + write(CLEAN_TEXT, { x: 10, y: startY + 80 }, null, 200, false); + + } else if (testMode === 1) { + // Test 2: Color-coded text (the problematic case) + ink("red").write("TEST 2: Timing-based blinking (the bug):", 10, startY); + + // This should demonstrate the blinking character issue + write(TIMING_TEXT, { x: 10, y: startY + 20 }, null, 200, false); + + // Show what it should look like (clean text for comparison) + ink("gray").write("Should be stable like this:", 10, startY + 60); + ink("white").write(CLEAN_TEXT, 10, startY + 80); + + // Analysis + ink("yellow").write("The 't' in 'test' and '3' in '123' should NOT blink!", 10, startY + 100); + ink("orange").write("This is caused by timing expressions in color codes.", 10, startY + 120); + + // Character by character breakdown + ink("yellow").write("Character positions should be:", 10, startY + 140); + const chars = "fade:red-blue-black-blue-red"; + for (let i = 0; i < Math.min(chars.length, 20); i++) { + const x = 10 + i * 6; + ink(i % 3 === 0 ? "red" : i % 3 === 1 ? "green" : "blue") + .write(chars[i], x, startY + 160); + } + + } else if (testMode === 2) { + // Test 3: Buffer compositing test + ink("magenta").write("TEST 3: Buffer compositing test:", 10, startY); + + // Create a small buffer and render text to it + const buffer = painting(150, 50, (p) => { + p.wipe(0, 0, 0, 0); // Transparent background + p.ink("white").write("Buffer text", 0, 0); + p.ink("red").write(COLOR_CODED_TEXT, 0, 20); + }); + + paste(buffer, 10, startY + 20); + + // Show buffer dimensions + ink("yellow").write(`Buffer: ${buffer.width}×${buffer.height}px`, 10, startY + 80); + } + + // Debug info + ink("gray").write(`Frame: ${frameCount}`, 10, screen.height - 30); + ink("gray").write(`Screen: ${screen.width}×${screen.height}px`, 10, screen.height - 15); +} + +function act({ event: e, help }) { + if (e.is("keyboard")) { + if (e.key === "1") { + testMode = 0; + help.choose(["clean"]); + } else if (e.key === "2") { + testMode = 1; + help.choose(["colors"]); + } else if (e.key === "3") { + testMode = 2; + help.choose(["buffer"]); + } + } +} + +export { boot, paint, act }; \ No newline at end of file diff --git a/system/public/aesthetic.computer/lib/disk.mjs b/system/public/aesthetic.computer/lib/disk.mjs index 95dbfd8835..66cdf0e047 100644 --- a/system/public/aesthetic.computer/lib/disk.mjs +++ b/system/public/aesthetic.computer/lib/disk.mjs @@ -48,6 +48,22 @@ import { isKidlispSource, fetchCachedCode, getCachedCode, initPersistentCache, g import { qrcode as qr, ErrorCorrectLevel } from "../dep/@akamfoad/qr/qr.mjs"; import { microtype, MatrixChunky8 } from "../disks/common/fonts.mjs"; +function matrixDebugEnabled() { + if (typeof window !== "undefined" && window?.acMatrixDebug) return true; + if (typeof globalThis !== "undefined" && globalThis?.acMatrixDebug) + return true; + return false; +} + +if (typeof globalThis !== "undefined") { + if (globalThis.acMatrixDebug === undefined) { + globalThis.acMatrixDebug = true; + } + if (typeof window !== "undefined" && window.acMatrixDebug === undefined) { + window.acMatrixDebug = globalThis.acMatrixDebug; + } +} + // Helper function to safely check for sandboxed environments in both main thread and worker contexts function isSandboxed() { try { @@ -114,6 +130,119 @@ let tf; // Active typeface global. // Cache for loaded typefaces to avoid recreating them const typefaceCache = new Map(); +const DEFAULT_TYPEFACE_BLOCK_WIDTH = 6; +const DEFAULT_TYPEFACE_BLOCK_HEIGHT = 10; +const HUD_LABEL_TEXT_MARGIN = 2; + +function resolveTypefaceInstance(typefaceRef) { + if (!typefaceRef) return undefined; + if (typefaceRef instanceof Typeface) return typefaceRef; + if (typeof typefaceRef === "string") { + if (typefaceCache.has(typefaceRef)) { + return typefaceCache.get(typefaceRef); + } + const instance = new Typeface(typefaceRef); + typefaceCache.set(typefaceRef, instance); + return instance; + } + if (typeof typefaceRef === "object" && typeof typefaceRef?.getGlyph === "function") { + return typefaceRef; + } + return undefined; +} + +function ensureTypefaceLoaded(typeface) { + if (!typeface || typeof typeface.load !== "function") return; + const requiresLoad = + typeface.data?.bdfFont || + typeface.name === "unifont" || + typeface.name === "MatrixChunky8"; + + if (!requiresLoad) return; + + if (!typeface.__loadPromise) { + typeface.__loadPromise = typeface.load($commonApi.net.preload, () => { + if (typeof window !== "undefined" && window.$activePaintApi?.needsPaint) { + window.$activePaintApi.needsPaint(); + } + }); + } + + return typeface.__loadPromise; +} + +function getTypefaceForMeasurement(typefaceName) { + if (!typefaceName) return tf; + const resolved = resolveTypefaceInstance(typefaceName); + return resolved || tf; +} + +function writeHudLabelText( + $, + text, + { x = 0, y = 0, typefaceName, preserveColors = true } = {}, +) { + if (text === undefined || text === null || text === "") return; + + const content = preserveColors ? text : stripColorCodes(text); + $.write( + content, + { x, y }, + undefined, + undefined, + false, + typefaceName, + ); +} + +function drawHudLabelText( + $, + text, + { + x = 0, + y = 0, + typefaceName, + textColor = "white", + shadowColor = "black", + shadowOffsetX = 1, + shadowOffsetY = 1, + preserveColors = true, + } = {}, +) { + if (!text) return; + + const containsColorCodes = textContainsColorCodes(text); + const shouldPreserveColors = preserveColors || (typefaceName === "MatrixChunky8" && containsColorCodes); + + + const shouldRenderShadow = shadowColor && !( + typefaceName === "MatrixChunky8" && + matrixDebugEnabled() + ); + + if (shouldRenderShadow) { + $.ink(shadowColor); + writeHudLabelText($, text, { + x: x + shadowOffsetX, + y: y + shadowOffsetY, + typefaceName, + preserveColors: false, + }); + } + + if (textColor) { + $.ink(textColor); + } + + writeHudLabelText($, text, { + x, + y, + typefaceName, + preserveColors: shouldPreserveColors, + }); + +} + export const noWorker = { onMessage: undefined, postMessage: undefined }; let ROOT_PIECE = "prompt"; // This gets set straight from the host html file for the ac. @@ -151,7 +280,7 @@ function storePersistentFirstLineColor(color) { // 🎨 Global function to get background color for reframe (worker-safe) function getPersistentFirstLineColor() { - console.log("🎨 Getting persistent first line color from disk.mjs:", persistentFirstLineColor); + return persistentFirstLineColor; } @@ -548,10 +677,21 @@ let currentPath, currentHUDStatusColor = "red", currentHUDButton, currentHUDScrub = 0, + currentHUDLabelFontName, + currentHUDLabelBlockWidth = tf?.blockWidth ?? DEFAULT_TYPEFACE_BLOCK_WIDTH, + currentHUDLabelBlockHeight = tf?.blockHeight ?? DEFAULT_TYPEFACE_BLOCK_HEIGHT, + currentHUDShareWidth = (tf?.blockWidth ?? DEFAULT_TYPEFACE_BLOCK_WIDTH) * "share ".length, + currentHUDLabelMeasuredWidth = 0, currentHUDOffset, qrOverlayCache = new Map(), // Cache for QR overlays to prevent regeneration every frame hudLabelCache = null; // Cache for HUD label to prevent regeneration every frame +let lastMatrixChunkyHighlightLog = null; +let lastMatrixChunkyWriteDiagnosticLog = null; +let lastMatrixChunkyPixelLog = null; + + + // Helper functions to safely access window flags in both main thread and worker contexts function isQROverlayCacheDisabled() { try { @@ -586,6 +726,9 @@ if (typeof window !== 'undefined') { } //currentPromptButton; +const COLOR_CODE_MATCH_REGEX = /\\([a-zA-Z]+(?:\([^)]*\))?|[0-9]+(?:,[0-9]+)*)\\/g; +const COLOR_CODE_TEST_REGEX = /\\([a-zA-Z]+(?:\([^)]*\))?|[0-9]+(?:,[0-9]+)*)\\/; + // Utility function to strip color codes from text function stripColorCodes(str) { if (!str) return str; @@ -594,11 +737,46 @@ function stripColorCodes(str) { // - RGB values: \\255,20,147\\, \\192,192,192\\ // - Complex patterns: \\color(args)\\ return str.replace( - /\\([a-zA-Z]+(?:\([^)]*\))?|[0-9]+(?:,[0-9]+)*)\\/g, + COLOR_CODE_MATCH_REGEX, "", ); } +function textContainsColorCodes(str) { + if (!str) return false; + return COLOR_CODE_TEST_REGEX.test(str); +} + +function hasKidLispMarkers(text) { + if (!text) return false; + return ( + text.includes("ink ") || + text.includes("line ") || + text.includes("box ") || + text.includes("circle ") || + text.includes("spin ") || + text.includes("(") || + textContainsColorCodes(text) || + /\d+s\.\.\./.test(text) || + /\?\s/.test(text) + ); +} + +function detectKidLispPiece({ currentPath, currentHUDTxt, currentText, cleanText }) { + const sourceCode = currentText || currentHUDTxt; + + return ( + (currentPath && lisp.isKidlispSource(currentPath) && !currentPath.endsWith('.lisp')) || + currentPath === "(...)" || + (sourceCode && sourceCode.startsWith("$")) || + (currentPath && (currentPath.includes("/disks/$") || currentPath.includes("$"))) || + (sourceCode && lisp.isKidlispSource && lisp.isKidlispSource(sourceCode)) || + hasKidLispMarkers(currentHUDTxt) || + textContainsColorCodes(cleanText) || + hasKidLispMarkers(sourceCode) + ); +} + function updateHUDStatus() { if (udp.connected && socket?.connected) { currentHUDStatusColor = "lime"; @@ -1253,7 +1431,7 @@ const $commonApi = { jump: function jump(to, ahistorical = false, alias = false) { // let url; - + if (leaving) { console.log("🚪🐴 Jump cancelled, already leaving..."); return; @@ -1600,13 +1778,31 @@ const $commonApi = { if (currentHUDTxt && currentHUDTxt.length > 0) { // Use plain text (without color codes) for dimension calculations const textForMeasurement = currentHUDPlainTxt || currentHUDTxt; + + // Detect if this is a KidLisp piece for more generous width allowance + const sourceCode = currentText || currentHUDTxt; + const isKidlispPiece = (currentPath && lisp?.isKidlispSource && lisp.isKidlispSource(currentPath) && !currentPath.endsWith('.lisp')) || + currentPath === "(...)" || + (sourceCode && sourceCode.startsWith("$")) || + (currentPath && currentPath.includes("/disks/$")) || + (sourceCode && lisp?.isKidlispSource && lisp.isKidlispSource(sourceCode)); + + // Use full screen width for text wrapping + const maxWidth = cachedAPI.screen.width; + const labelBounds = cachedAPI.text.box( textForMeasurement, undefined, - cachedAPI.screen.width - cachedAPI.typeface.blockWidth, + maxWidth, + 1, // scale + true, // wordWrap + // TODO: This should check useTinyHudLabel like the main corner label, but that's not accessible here + // For now, use default font to maintain consistency with current behavior + undefined // Use default font - will need to be updated when useTinyHudLabel is globally accessible ); - let w = textForMeasurement.length * cachedAPI.typeface.blockWidth + currentHUDScrub; + // Use the actual computed width from text.box instead of character count + let w = labelBounds.box.width + currentHUDScrub; const h = labelBounds.box.height + cachedAPI.typeface.blockHeight; // Store dimensions for animation calculations @@ -2088,135 +2284,223 @@ const $commonApi = { return 10; }, // Return a text's bounding box. - box: (text, pos = { x: 0, y: 0 }, bounds, scale = 1, wordWrap = true) => { + box: (text, pos = { x: 0, y: 0 }, bounds, scale = 1, wordWrap = true, fontName) => { + if (!text) { console.warn("⚠️ No text for `box`."); return; } - - // DEBUG: Log what text.box is processing (reduced frequency) + pos = { ...pos }; - let run = 0; - const blockWidth = tf.blockWidth * abs(scale); + const absScale = abs(scale ?? 1); - const lines = [[]]; - let line = 0; + let useTypeface = getTypefaceForMeasurement(fontName) || tf; + if (!useTypeface) { + useTypeface = tf; + } - if (bounds === undefined) bounds = (text.length + 2) * blockWidth; + const baseBlockWidth = + useTypeface?.blockWidth ?? tf?.blockWidth ?? DEFAULT_TYPEFACE_BLOCK_WIDTH; + const baseBlockHeight = + useTypeface?.blockHeight ?? tf?.blockHeight ?? DEFAULT_TYPEFACE_BLOCK_HEIGHT; - function newLine() { - run = 0; - line += 1; - lines[line] = []; + const blockHeight = baseBlockHeight * absScale; + + const isProportional = + useTypeface?.data?.proportional === true || + !!useTypeface?.data?.advances || + !!useTypeface?.data?.bdfFont; + + const getAdvanceWidth = (char) => { + if (!char) return baseBlockWidth * absScale; + if (isProportional && typeof useTypeface?.getAdvance === "function") { + const raw = useTypeface.getAdvance(char); + if (typeof raw === "number") { + return raw * absScale; + } + } + return baseBlockWidth * absScale; + }; + + const getCharWidth = (char) => { + if (!char || char === "\n" || char === "\r") return 0; + if (char === "\t") { + return getAdvanceWidth(" ") * 4; + } + return getAdvanceWidth(char); + }; + + if (bounds === undefined) { + const sampleWidth = getAdvanceWidth("0"); + bounds = (text.length + 2) * sampleWidth; + } + if (!(bounds > 0)) { + bounds = Number.POSITIVE_INFINITY; } - function characterWrap(word, preserveSpaceBefore = false) { - let needsSpace = preserveSpaceBefore && run > 0; + const lines = [""]; + const charMap = [[]]; + let line = 0; + let run = 0; + let maxWidth = 0; + + const commitLineWidth = () => { + if (run > maxWidth) { + maxWidth = run; + } + }; - for (let i = 0; i < word.length; i++) { - const char = word[i]; - const charLen = blockWidth; - const spaceLen = needsSpace ? blockWidth : 0; + const newLine = () => { + commitLineWidth(); + line += 1; + lines[line] = ""; + charMap[line] = []; + run = 0; + }; - if (run + spaceLen + charLen > bounds) { + const appendChar = (char, sourceIndex) => { + const width = getCharWidth(char); + const renderedChar = char === "\t" ? " " : char; + lines[line] += renderedChar; + charMap[line].push(sourceIndex); + run += width; + if (run > maxWidth) { + maxWidth = run; + } + }; + + if (!wordWrap) { + for (let idx = 0; idx < text.length; idx += 1) { + const char = text[idx]; + if (char === "\r") continue; + if (char === "\n") { newLine(); - needsSpace = false; // Don't add space at start of new line + continue; } - - if (!lines[line].length) { - // Start of line - add space if needed, then character - if (needsSpace) { - lines[line].push(" " + char); - run += spaceLen + charLen; - needsSpace = false; - } else { - lines[line].push(char); - run += charLen; - } - } else { - // Continuing existing word - add space if needed, then character - if (needsSpace) { - lines[line][lines[line].length - 1] += " " + char; - run += spaceLen + charLen; - needsSpace = false; - } else { - lines[line][lines[line].length - 1] += char; - run += charLen; + appendChar(char, idx); + } + } else { + const tokens = []; + let idx = 0; + while (idx < text.length) { + const char = text[idx]; + if (char === "\r") { + idx += 1; + continue; + } + if (char === "\n") { + tokens.push({ type: "newline", indices: [idx] }); + idx += 1; + continue; + } + if (char === " " || char === "\t") { + let textBuffer = ""; + const indices = []; + while (idx < text.length) { + const c = text[idx]; + if (c !== " " && c !== "\t") break; + textBuffer += c; + indices.push(idx); + idx += 1; } + tokens.push({ type: "whitespace", text: textBuffer, indices }); + continue; } - } - } - if (wordWrap) { - const splitWords = text.split(" "); - const words = []; - for (let i = 0; i < splitWords.length; i++) { - if (splitWords[i] === "" && i > 0 && splitWords[i - 1] === "") { - words[words.length - 1] += " "; - } else { - words.push(splitWords[i]); + let textBuffer = ""; + const indices = []; + while (idx < text.length) { + const c = text[idx]; + if (c === "\n" || c === " " || c === "\t" || c === "\r") break; + textBuffer += c; + indices.push(idx); + idx += 1; + } + if (textBuffer.length > 0) { + tokens.push({ type: "word", text: textBuffer, indices }); } } - words.forEach((word, wordIndex) => { - const wordLen = word.length * blockWidth; - const spaceWidth = blockWidth; - - if (wordLen >= bounds) { - // Pass true to preserve space if this isn't the first word - characterWrap(word, wordIndex > 0); - // Don't add extra space width after character wrapping + + tokens.forEach((token) => { + if (token.type === "newline") { + newLine(); return; } - if (word.includes("\n")) { - const segs = word.split("\n"); - segs.forEach((seg, i) => { - const segLen = seg.length * blockWidth; - // For segments after a newline (i > 0), always start a new line - if (i > 0) newLine(); - // For boundary checking, account for space only if this isn't the first item on the line - const spaceNeeded = run > 0 ? spaceWidth : 0; - if (run + spaceNeeded + segLen >= bounds) newLine(); - lines[line].push(seg); - run += segLen + (wordIndex < words.length - 1 ? spaceWidth : 0); // Only add space if not the last word + + if (token.type === "whitespace") { + for (let i = 0; i < token.text.length; i += 1) { + const char = token.text[i]; + const sourceIndex = token.indices[i]; + const width = getCharWidth(char); + if (run > 0 && run + width > bounds) { + newLine(); + } + appendChar(char, sourceIndex); + } + return; + } + + if (token.type === "word") { + const chars = token.text.split(""); + const widths = chars.map((char) => getCharWidth(char)); + const wordWidth = widths.reduce((acc, value) => acc + value, 0); + + if (bounds > 0 && wordWidth > bounds) { + chars.forEach((char, charIdx) => { + const width = widths[charIdx]; + if (run > 0 && run + width > bounds) { + newLine(); + } + appendChar(char, token.indices[charIdx]); + }); + return; + } + + if (run > 0 && run + wordWidth > bounds) { + newLine(); + } + + chars.forEach((char, charIdx) => { + appendChar(char, token.indices[charIdx]); }); - } else { - // For boundary checking, account for space only if this isn't the first word on the line - const spaceNeeded = run > 0 ? spaceWidth : 0; - if (run + spaceNeeded + wordLen >= bounds) newLine(); - lines[line].push(word); - run += wordLen + (wordIndex < words.length - 1 ? spaceWidth : 0); // Only add space if not the last word } }); - } else { - characterWrap(text); } - const blockMargin = 0; - const lineHeightGap = 1; // Slightly less than write function for tighter fit - const blockHeight = (tf.blockHeight + lineHeightGap) * scale; // 10 + 1 = 11px per line + commitLineWidth(); + + const lineHeightGap = 1 * absScale; + const finalBlockHeight = blockHeight + lineHeightGap; if (lines.length >= 1 && pos.center && pos.center.indexOf("y") !== -1) { pos.y = $activePaintApi.screen.height / 2 - - (lines.length * blockHeight) / 2 + - blockHeight / 2 + + (lines.length * finalBlockHeight) / 2 + + finalBlockHeight / 2 + (pos.y || 0); } - const height = lines.length * blockHeight; - - // Calculate the actual width of the longest line (true text bounds) - let maxLineWidth = 0; - lines.forEach(line => { - if (line.length > 0) { - const lineText = line.join(' '); // Join words with spaces - const lineWidth = lineText.length * blockWidth; - maxLineWidth = Math.max(maxLineWidth, lineWidth); + const height = lines.length * finalBlockHeight; + + const lineWidths = lines.map((lineText, index) => { + if (!lineText) return 0; + const indices = charMap[index] || []; + let width = 0; + for (let i = 0; i < lineText.length; i += 1) { + const sourceIndex = indices[i]; + if (typeof sourceIndex === "number" && sourceIndex >= 0 && sourceIndex < text.length) { + width += getCharWidth(text[sourceIndex]); + } else { + width += getCharWidth(lineText[i]); + } } + return width; }); - + + const maxLineWidth = lineWidths.reduce((acc, value) => Math.max(acc, value), 0); + const box = { x: pos.x, y: pos.y, width: maxLineWidth, height }; - if (lines[0]?.[0].startsWith("test:")) console.log(lines); - return { pos, box, lines }; + + return { pos, box, lines, lineWidths, lineHeight: finalBlockHeight, charMap }; }, }, num: { @@ -3206,34 +3490,20 @@ const $paintApi = { customTypeface = arguments[5]; } - // Convert string font name to Typeface instance if needed - if (typeof customTypeface === "string") { - // Check if we have a cached typeface first - if (typefaceCache.has(customTypeface)) { - customTypeface = typefaceCache.get(customTypeface); - } else { - // Create a new typeface and load it immediately if it's MatrixChunky8 or unifont - const newTypeface = new Typeface(customTypeface); - - // Load the typeface asynchronously for BDF fonts - if (newTypeface.data?.bdfFont || customTypeface === "unifont") { - newTypeface.load($commonApi.net.preload, () => { - // Force repaint when new glyphs are loaded - if (typeof window !== 'undefined' && window.$activePaintApi?.needsPaint) { - window.$activePaintApi.needsPaint(); - } - }); - } - - typefaceCache.set(customTypeface, newTypeface); - customTypeface = newTypeface; + if (customTypeface) { + const resolvedTypeface = resolveTypefaceInstance(customTypeface); + if (resolvedTypeface) { + ensureTypefaceLoaded(resolvedTypeface); + customTypeface = resolvedTypeface; } - } // 🎨 Color code processing - // Check for color codes like \\blue\\, \\red\\, \\255,0,0\\, etc. - const colorCodeRegex = /\\([a-zA-Z0-9,]+)\\/g; - const hasColorCodes = text.includes("\\"); + } + + // 🎨 Color code processing + const hasColorCodes = textContainsColorCodes(text); if (hasColorCodes) { + + // Remember the current ink color to restore it later const originalColor = $activePaintApi.inkrn(); @@ -3243,7 +3513,11 @@ const $paintApi = { let currentColor = null; // Split text by color codes and process each segment - const segments = text.split(colorCodeRegex); + const segments = text.split(COLOR_CODE_MATCH_REGEX); + + + + for (let i = 0; i < segments.length; i++) { if (i % 2 === 0) { @@ -3252,89 +3526,100 @@ const $paintApi = { for (let j = 0; j < segment.length; j++) { cleanText += segment[j]; charColors.push(currentColor); + + } } else { // This is a color name or RGB value (from the captured group) const colorStr = segments[i]; - - // Handle different color formats - if (colorStr.includes(',')) { - // RGB/RGBA format like "255,0,0" or "255,0,0,128" - const parts = colorStr.split(',').map(n => parseInt(n.trim())); - currentColor = parts; - } else if (colorStr === 'transparent') { - // Handle transparent color + + if (!colorStr) { + continue; + } + + const normalized = colorStr.trim(); + const lower = normalized.toLowerCase(); + + if (!normalized) { + continue; + } + + if (lower === "reset" || lower === "default" || lower === "base") { currentColor = null; + continue; + } + + if (normalized.includes(",")) { + // RGB/RGBA format like "255,0,0" or "255,0,0,128" + const parts = normalized.split(",").map((n) => { + const parsed = parseInt(n.trim(), 10); + return Number.isFinite(parsed) ? parsed : 0; + }); + while (parts.length < 3) parts.push(0); + if (parts.length === 3) parts.push(255); + currentColor = parts.slice(0, 4); + } else if (lower === "transparent" || lower === "clear") { + currentColor = [0, 0, 0, 0]; } else { - // Named color like "red", "blue", etc. - currentColor = colorStr; + const resolved = graph.findColor(normalized); + if (Array.isArray(resolved)) { + currentColor = resolved.slice(); + } else if (resolved && typeof resolved === "object") { + currentColor = { ...resolved }; + } else if (resolved !== undefined) { + currentColor = resolved; + } else { + currentColor = null; + } } + + } } - // Check if we have any actual text to display after removing color codes + // Check if we have any actual text to display after removing color codes if (cleanText.trim().length === 0) { return $activePaintApi; // Exit silently if no text content remains } - // Now use the original text processing logic but with per-character colors + + + // Render with colors - same logic as original text processing const scale = pos?.size || 1; - - // Debug what activeTypeface is being used (disabled) - // if (text.includes("$") || (arguments[5] === "MatrixChunky8")) { - // console.log("🔤 activeTypeface determined:", { - // customTypeface: customTypeface?.name || String(customTypeface), - // tf: tf?.name || String(tf), - // activeTypeface: tf?.name || String(tf), // Use tf instead of undefined activeTypeface - // isCustomTypefaceObject: typeof customTypeface === 'object' && customTypeface !== null, - // customTypefaceType: typeof customTypeface - // }); - // } - - // Debug logging for QR text rendering (disabled) - // if (text.includes("$") || (arguments[5] === "MatrixChunky8")) { - // console.log("🔤 QR Font Debug:", { - // text: text, - // textLength: text.length, - // requestedFont: arguments[5], - // customTypeface: customTypeface?.name || String(customTypeface), - // customTypefaceType: typeof customTypeface, - // tfName: tf?.name || String(tf), - // activeTypeface: tf?.name || String(tf), // Use tf instead of undefined activeTypeface - // activeTypefaceData: tf?.data?.bdfFont || "no-bdf", // Use tf instead of undefined activeTypeface - // hasCustomTypeface: !!customTypeface, - // hasTf: !!tf, - // activetypefacePrint: typeof tf?.print, // Use tf instead of undefined activeTypeface - // typefaceCacheSize: typefaceCache.size, - // typefaceCacheKeys: Array.from(typefaceCache.keys()), - // isMatrixChunky8: tf?.name === "MatrixChunky8", // Use tf instead of undefined activeTypeface - // firstCharGlyph: tf?.glyphs?.[text[0]] ? "loaded" : "missing" // Use tf instead of undefined activeTypeface - // }); - // } if (bounds) { const tb = $commonApi.text.box(cleanText, pos, bounds, scale, wordWrap, customTypeface); if (!tb || !tb.lines) { return $activePaintApi; // Exit silently if text.box fails } - tb.lines.forEach((line, index) => { - // Calculate the starting character index for this line - let lineStartIndex = 0; - for (let i = 0; i < index; i++) { - lineStartIndex += tb.lines[i].join(" ").length; - if (i < tb.lines.length - 1) lineStartIndex++; // Add 1 for space between lines + + const charMap = tb.charMap || []; + + tb.lines.forEach((lineText, index) => { + const renderedLine = typeof lineText === "string" ? lineText : lineText?.join?.(" ") || ""; + const sourceIndices = charMap[index] || []; + const lineColors = []; + + for (let i = 0; i < renderedLine.length; i++) { + const sourceIndex = sourceIndices[i]; + if ( + typeof sourceIndex === "number" && + sourceIndex >= 0 && + sourceIndex < charColors.length + ) { + lineColors.push(charColors[sourceIndex]); + } else { + lineColors.push(null); + } } (customTypeface || tf)?.print( $activePaintApi, tb.pos, index, - line.join(" "), + renderedLine, bg, - charColors.slice( - lineStartIndex, - lineStartIndex + line.join(" ").length, - ), + lineColors, ); }); } else { @@ -3343,8 +3628,9 @@ const $paintApi = { const lines = cleanText.split("\n"); const lineHeightGap = 2; let charIndex = 0; + lines.forEach((line, index) => { - const lineColors = charColors.slice( + const lineColors = charColors?.slice( charIndex, charIndex + line.length, ); @@ -3368,9 +3654,34 @@ const $paintApi = { } } + const typefaceName = typeof customTypeface === "string" ? customTypeface : customTypeface?.name; + if (typefaceName === "MatrixChunky8") { + const hasColorAssignments = charColors?.some((color) => { + if (!color) return false; + if (Array.isArray(color)) { + return color.some((component) => component !== null && component !== undefined); + } + if (typeof color === "object") { + return Object.keys(color).length > 0; + } + return typeof color === "string"; + }); + + if (!hasColorAssignments) { + const snippet = cleanText.length > 120 ? `${cleanText.slice(0, 117)}…` : cleanText; + if (lastMatrixChunkyWriteDiagnosticLog !== snippet) { + lastMatrixChunkyWriteDiagnosticLog = snippet; + console.warn("🎨 MatrixChunky8 HUD charColors missing", { + snippet, + charColorsLength: charColors?.length, + cleanTextLength: cleanText.length, + }); + } + } + } + // Restore the original ink color $activePaintApi.ink(...originalColor); - return $activePaintApi; } @@ -3382,9 +3693,9 @@ const $paintApi = { if (bounds) { const tb = $commonApi.text.box(text, pos, bounds, scale, wordWrap, customTypeface); // TODO: Get the current ink color, memoize it, and make it static here. // 23.10.12.22.04 - tb.lines.forEach((line, index) => { - // console.log(line, index); - (customTypeface || tf)?.print($activePaintApi, tb.pos, index, line.join(" "), bg); + tb.lines.forEach((lineText, index) => { + const renderedLine = typeof lineText === "string" ? lineText : lineText?.join?.(" ") || ""; + (customTypeface || tf)?.print($activePaintApi, tb.pos, index, renderedLine, bg); }); } else { // Break on `\n` and handle separate lines @@ -4272,7 +4583,7 @@ function executeLispCode(source, api, isAccumulating = false) { // console.log(`🔍 Parsing KidLisp source:`, source); // Clear previous first-line color state for fresh detection - console.log("🎨 executeLispCode: Clearing globalKidLispInstance.firstLineColor"); + globalKidLispInstance.firstLineColor = null; globalKidLispInstance.parse(source); @@ -4281,7 +4592,7 @@ function executeLispCode(source, api, isAccumulating = false) { if (globalKidLispInstance.ast) { // Detect first-line color but only apply it if not accumulating globalKidLispInstance.detectFirstLineColor(); - console.log("🎨 executeLispCode: After detectFirstLineColor, globalKidLispInstance.firstLineColor:", globalKidLispInstance.firstLineColor); + if (globalKidLispInstance.firstLineColor && !isAccumulating) { // console.log(`🎨 Applying first-line color background: ${globalKidLispInstance.firstLineColor}`); api.wipe(globalKidLispInstance.firstLineColor); @@ -4353,7 +4664,6 @@ class Painting { // ⛓️ This wrapper also makes the paint API chainable. function globals(k, args) { - if (k === "ink") p.inkrn = [...args].flat(); if (k === "page") p.pagern = args[0]; // TODO: 😅 Add other state globals like line thickness? 23.1.25 } @@ -4362,16 +4672,27 @@ class Painting { if (typeof $paintApiUnwrapped[k] === "function") { // Wrap and then transfer to #api. p.api[k] = function () { - globals(k, arguments); // Keep track of global state, like ink, via `inkrn`. + if (k === "ink") { + $paintApiUnwrapped[k](...arguments); + p.inkrn = graph.c.slice(); + } else { + globals(k, arguments); // Keep track of global state, like page, via `pagern`. + } // Create layer if necessary. if (notArray(p.#layers[p.#layer])) p.#layers[p.#layer] = []; + const callArgs = arguments; // Add each deferred paint api function to the layer, to be run // all at once in `paint` on each frame update. p.#layers[p.#layer].push([ k, () => { - globals(k, arguments); // Update globals again on chainable calls. - $paintApiUnwrapped[k](...arguments); + if (k === "ink") { + $paintApiUnwrapped[k](...callArgs); + p.inkrn = graph.c.slice(); + } else { + globals(k, callArgs); // Update globals again on chainable calls. + $paintApiUnwrapped[k](...callArgs); + } }, ]); return p.api; @@ -4582,7 +4903,7 @@ $commonApi.resolution = function (width, height = width, gap = 8) { // Try to get color from KidLisp instance as fallback if (typeof globalKidLispInstance?.getBackgroundFillColor === 'function') { const fallbackColor = globalKidLispInstance.getBackgroundFillColor(); - console.log("🎨 Post-reframe: KidLisp fallback color:", fallbackColor); + if (fallbackColor) { const colorResult = graph.color.coerce(fallbackColor); @@ -5717,6 +6038,11 @@ async function load( $commonApi.typeface = tf; // Expose a preloaded typeface globally. ui.setTypeface(tf); // Set the default `ui` typeface. + // Initialize HUD metrics now that the primary typeface is available + currentHUDLabelBlockWidth = tf.blockWidth; + currentHUDLabelBlockHeight = tf.blockHeight; + currentHUDShareWidth = tf.blockWidth * "share ".length; + // Initialize MatrixChunky8 font for QR code text rendering // TEMP: Clear MatrixChunky8 cache to force reload with new J advance width if (typefaceCache.has("MatrixChunky8")) { @@ -8380,11 +8706,11 @@ async function makeFrame({ data: { type, content } }) { currentHUDScrub += e.delta.x; } - const shareWidth = tf.blockWidth * "share ".length; + const shareWidth = currentHUDShareWidth || (currentHUDLabelBlockWidth * "share ".length); if (currentHUDScrub >= 0) { - btn.box.w = - tf.blockWidth * currentHUDTxt.length + currentHUDScrub; + const baseWidth = hudAnimationState.labelWidth ?? currentHUDLabelMeasuredWidth ?? (currentHUDLabelBlockWidth * (currentHUDPlainTxt?.length || currentHUDTxt.length)); + btn.box.w = baseWidth + currentHUDScrub; // console.log(btn.b); } @@ -8407,8 +8733,6 @@ async function makeFrame({ data: { type, content } }) { currentHUDTextColor = originalColor; const shareWidth = tf.blockWidth * "share ".length; - console.log("scrub:", currentHUDScrub, shareWidth); - if (currentHUDScrub === shareWidth) { $api.sound.synth({ tone: 1800, @@ -9169,145 +9493,219 @@ async function makeFrame({ data: { type, content } }) { // Remove any remaining single backslashes that might be color code remnants cleanText = cleanText.replace(/\\/g, ''); - // 🔵 DEBUG: Enhanced text cleaning logging with samples - // Use consistent scaling regardless of screen size - const screenScale = 1.0; - const scaledBlockWidth = tf.blockWidth * screenScale; - - // Account for 6px margin on left and right (button starts at x=6) - const maxButtonWidth = $api.screen.width - 12; - - // First get natural dimensions with a generous but reasonable width limit - const naturalMaxWidth = Math.max($api.screen.width * 2, 600); // Allow up to 2x screen width or 600px minimum - const naturalBounds = $api.text.box( + // Analyze visible lines for accurate sizing + const rawLines = cleanText.split('\n'); + const visibleLines = rawLines.map((line) => line.replace(/\s+$/, '')); + const visibleLineCount = visibleLines.length; + + const defaultTypeface = tf; + const matrixTypeface = typefaceCache.get("MatrixChunky8"); + + const isKidlispPiece = detectKidLispPiece({ + currentPath, + currentHUDTxt, + currentText, cleanText, - undefined, - naturalMaxWidth, // Use reasonable max width instead of undefined - screenScale - ); - - // Check if natural width fits screen, if not use constrained version - let textBounds; - if (naturalBounds.box.width <= $api.screen.width - 20) { - // Use natural dimensions - no wrapping needed - textBounds = naturalBounds; - } else { - // Use constrained dimensions for screen fit - textBounds = $api.text.box( + }); + + const measureLineWidth = (line, typeface) => { + if (!line || !line.length || !typeface) return 0; + const advances = typeface.data?.advances; + const defaultAdvance = typeface.blockWidth || DEFAULT_TYPEFACE_BLOCK_WIDTH; + let width = 0; + for (let i = 0; i < line.length; i++) { + const ch = line[i]; + width += advances && advances[ch] !== undefined ? advances[ch] : defaultAdvance; + } + return width; + }; + + const buildLayout = (typeface) => { + if (!typeface) return null; + + const fontIdentifier = typeface === defaultTypeface ? undefined : typeface.name; + const blockWidth = typeface.blockWidth || DEFAULT_TYPEFACE_BLOCK_WIDTH; + const blockHeight = typeface.blockHeight || DEFAULT_TYPEFACE_BLOCK_HEIGHT; + const lineWidths = visibleLines.map((line) => measureLineWidth(line, typeface)); + const longestLineWidth = lineWidths.reduce((max, width) => Math.max(max, width), 0); + const bounds = isKidlispPiece ? 10000 : Math.max($api.screen.width * 4, 1000); + + const naturalBounds = $api.text.box( cleanText, - undefined, - maxButtonWidth, - screenScale + { x: 0, y: 0 }, + bounds, + 1, + !isKidlispPiece, + fontIdentifier, ); + + if (fontIdentifier === "MatrixChunky8") { + for (const line of visibleLines) { + for (let i = 0; i < line.length; i++) { + typeface.glyphs?.[line[i]]; + } + } + } + + return { + typeface, + fontIdentifier, + blockWidth, + blockHeight, + lineWidths, + longestLineWidth, + textBoxWidth: naturalBounds?.box?.width ?? 0, + textBoxHeight: naturalBounds?.box?.height ?? 0, + naturalBounds, + }; + }; + + const defaultLayout = buildLayout(defaultTypeface); + let selectedLayout = defaultLayout; + + if ( + isKidlispPiece && + matrixTypeface && + defaultLayout && + defaultLayout.longestLineWidth > $api.screen.width + ) { + const matrixLayout = buildLayout(matrixTypeface); + if (matrixLayout) { + selectedLayout = matrixLayout; + } } - - // Use the exact width from text.box - no padding, perfect text hugging - let w = textBounds.box.width; - - // DEBUG: Throttled HUD width calculations (every 30 frames) - DISABLED OLD VERSION - if (false && shouldLog) { - const lines = cleanText.split('\n'); - console.log("� Button Width: HUD Width Debug:", { - piece: piece, - screenWidth: $api.screen.width, - screenHeight: $api.screen.height, - cleanText: cleanText.substring(0, 50) + "...", - cleanTextLength: cleanText.length, - lineCount: lines.length, - longestLine: Math.max(...lines.map(line => line.length)), - tfBlockWidth: tf.blockWidth, - screenScale: screenScale, - scaledBlockWidth: scaledBlockWidth, - maxButtonWidth: maxButtonWidth, - textWidth: textWidth, - textBoxWidth: textBounds.box.width, - textBoxHeight: textBounds.box.height, - padding: padding, - finalWidth: w, - currentHUDScrub: currentHUDScrub, - isMultiline: cleanText.includes('\n'), - willOverflow: w > $api.screen.width - }); + + if (!selectedLayout && matrixTypeface) { + selectedLayout = buildLayout(matrixTypeface); + } + + if (!selectedLayout) { + selectedLayout = defaultLayout; } - // SIMPLIFIED: Use text.box output directly for both width and height - w = textBounds.box.width; - let h = textBounds.box.height; + const selectedTypeface = selectedLayout?.typeface || defaultTypeface; + const selectedHudFont = selectedLayout?.fontIdentifier; + const hudBlockWidth = selectedLayout?.blockWidth ?? DEFAULT_TYPEFACE_BLOCK_WIDTH; + const hudBlockHeight = selectedLayout?.blockHeight ?? DEFAULT_TYPEFACE_BLOCK_HEIGHT; + const textBounds = selectedLayout?.naturalBounds; + const textBoxWidth = selectedLayout?.textBoxWidth ?? 0; + const textBoxHeight = selectedLayout?.textBoxHeight ?? 0; + const longestVisibleLineWidth = selectedLayout?.longestLineWidth ?? 0; + + const measuredShareWidth = measureLineWidth("share ", selectedTypeface); + currentHUDShareWidth = measuredShareWidth > 0 + ? measuredShareWidth + : hudBlockWidth * "share ".length; + + currentHUDLabelFontName = selectedHudFont; + currentHUDLabelBlockWidth = hudBlockWidth; + currentHUDLabelBlockHeight = hudBlockHeight; + + // Prefer visible line width for KidLisp pieces to avoid oversized buffers + let w = isKidlispPiece ? longestVisibleLineWidth : textBoxWidth; + if (!w || w <= 0) { + w = textBoxWidth; + } + + // Final text dimensions: KidLisp width uses visible line metrics, height from text box + let h = textBoxHeight; if (piece === "video") w = screen.width; - // DEBUG: Simplified text.box results - if (false && currentHUDTxt && currentHUDTxt.length > 0 && (pieceFrameCount % 60 === 0)) { - console.log(`� text.box RESULT - Using: ${w}x${h}, MaxWidth: ${maxButtonWidth}`); - } - // SIMPLIFIED: No buffer expansion, painting size matches button size exactly - let bufferW = w; + // Use natural text dimensions for buffer - preserve original layout + let bufferW = w; // Keep natural width to maintain character positioning let bufferH = h; + // Ensure minimum buffer size for readability + const minBufferW = Math.max(isKidlispPiece ? longestVisibleLineWidth : textBoxWidth, 50); + const minBufferH = Math.max(hudBlockHeight, 20); // At least one line height + + if (bufferW < minBufferW) { + bufferW = minBufferW; + } + if (bufferH < minBufferH) { + bufferH = minBufferH; + } + + // Fix height calculation for KidLisp pieces when wordWrap=false + if (isKidlispPiece) { + // When wordWrap=false, text.box doesn't calculate height correctly for multi-line text + // Calculate proper height based on actual line count + const properHeight = visibleLineCount * hudBlockHeight; + if (properHeight > bufferH) { + bufferH = properHeight; + } + } + + const hudDescenderPadding = Math.max(2, Math.round(hudBlockHeight * 0.2)); + bufferH += hudDescenderPadding; + + // DEBUG: Log buffer dimensions in copy-pasteable format (reduced frequency) + if (false && isKidlispPiece && currentHUDTxt && pieceFrameCount % 120 === 0) { + console.log(`HUD_BUFFER: bufferW=${bufferW} bufferH=${bufferH} textBoxW=${textBounds.box.width} textBoxH=${textBounds.box.height} longestLinePx=${longestVisibleLineWidth} lines=${visibleLineCount} fontHeight=${hudBlockHeight} expectedH=${visibleLineCount * hudBlockHeight}`); + console.log(`HUD_TEXT: "${cleanText.replace(/\n/g, '\\n').substring(0, 150)}"`); + } + + + // Store actual dimensions for animation calculations - hudAnimationState.labelWidth = w; - hudAnimationState.labelHeight = h; + currentHUDLabelMeasuredWidth = bufferW; + hudAnimationState.labelWidth = bufferW; + hudAnimationState.labelHeight = bufferH; + h = bufferH; label = $api.painting(bufferW, bufferH, ($) => { // Ensure label renders with clean pan state $.unpan(); + + // Clean rendering - no debug elements + + // HUD Background Highlighting: Add background for kidlisp hud corner labels if (HIGHLIGHT_MODE) { - // Detect if this is a KidLisp piece - const sourceCode = currentText || currentHUDTxt; - const isKidlispPiece = (currentPath && lisp.isKidlispSource(currentPath) && !currentPath.endsWith('.lisp')) || - currentPath === "(...)" || - (sourceCode && sourceCode.startsWith("$")) || - (currentPath && currentPath.includes("/disks/$")) || - (sourceCode && lisp.isKidlispSource(sourceCode)); + // Get the actual text that will be rendered (same logic as below) + let text = currentHUDTxt; + if (currentHUDTxt.split(" ")[1]?.indexOf("http") !== 0) { + text = currentHUDTxt?.replaceAll("~", " "); + text = text?.replaceAll("§", " "); + } - + // Strip color codes to get the actual visible text + const colorCodeRegex = /\\[^\\]*\\/g; + const visibleText = text.replace(colorCodeRegex, ''); - // Apply highlighting to ALL pieces when HIGHLIGHT_MODE is enabled, not just KidLisp - if (true) { // Changed from isKidlispPiece to always true when HIGHLIGHT_MODE is on - // Get the actual text that will be rendered (same logic as below) - let text = currentHUDTxt; - if (currentHUDTxt.split(" ")[1]?.indexOf("http") !== 0) { - text = currentHUDTxt?.replaceAll("~", " "); - text = text?.replaceAll("§", " "); - } - - // Strip color codes to get the actual visible text - const colorCodeRegex = /\\[^\\]*\\/g; - const visibleText = text.replace(colorCodeRegex, ''); - - // Calculate snug dimensions based on visible characters only - const snugWidth = visibleText.length * tf.blockWidth; - const snugHeight = tf.blockHeight; - - // Position highlight with 2px padding on all sides around the text - const leftMargin = 2; // Define the left margin for HUD text - const bgX = leftMargin - 2; // Start 2px left of text (text at leftMargin = 2, so box at 0) - const bgY = -2; // Start 2px above text - const bgWidth = snugWidth + 4; // Text width + 2px padding on each side - const bgHeight = snugHeight + 4; // Text height + 2px padding top and bottom - - // Parse highlight color - support RGB comma format, hex, or named colors - let bgColor; - if (HIGHLIGHT_COLOR.includes(',')) { - const [r, g, b] = HIGHLIGHT_COLOR.split(',').map(n => parseInt(n.trim())); - bgColor = [r, g, b, 100]; // Semi-transparent - } else if (HIGHLIGHT_COLOR.startsWith('#')) { - // Parse hex color like #FFFF00 - const hex = HIGHLIGHT_COLOR.slice(1); // Remove # - const r = parseInt(hex.substring(0, 2), 16); - const g = parseInt(hex.substring(2, 4), 16); - const b = parseInt(hex.substring(4, 6), 16); - bgColor = [r, g, b, 100]; // Semi-transparent - } else { - // Use bright yellow as default for highlight - bgColor = [255, 255, 0, 100]; // Bright yellow, semi-transparent - } - - // Background highlight with configurable color - $.ink(...bgColor).box(bgX, bgY, bgWidth, bgHeight); + // Calculate snug dimensions based on visible characters only + const snugWidth = Math.max(longestVisibleLineWidth || 0, textBoxWidth); + const snugHeight = Math.max(hudBlockHeight, visibleLineCount * hudBlockHeight) + hudDescenderPadding; + + // Position highlight with 2px padding on all sides around the text + const highlightPadding = 2; + const leftMargin = HUD_LABEL_TEXT_MARGIN; // Define the left margin for HUD text + const bgX = leftMargin - highlightPadding; // Start padding pixels left of text origin + const bgY = -highlightPadding; // Start padding above text + const bgWidth = snugWidth + highlightPadding * 2; // Text width + padding on each side + const bgHeight = snugHeight + highlightPadding * 2; // Text height + padding top and bottom + + // Parse highlight color - support RGB comma format, hex, or named colors + let bgColor; + if (HIGHLIGHT_COLOR.includes(',')) { + const [r, g, b] = HIGHLIGHT_COLOR.split(',').map(n => parseInt(n.trim())); + bgColor = [r, g, b, 100]; // Semi-transparent + } else if (HIGHLIGHT_COLOR.startsWith('#')) { + // Parse hex color like #FFFF00 + const hex = HIGHLIGHT_COLOR.slice(1); // Remove # + const r = parseInt(hex.substring(0, 2), 16); + const g = parseInt(hex.substring(2, 4), 16); + const b = parseInt(hex.substring(4, 6), 16); + bgColor = [r, g, b, 100]; // Semi-transparent + } else { + // Use bright yellow as default for highlight + bgColor = [255, 255, 0, 100]; // Bright yellow, semi-transparent } + + // Background highlight with configurable color + $.ink(...bgColor).box(bgX, bgY, bgWidth, bgHeight); } let c; @@ -9327,93 +9725,32 @@ async function makeFrame({ data: { type, content } }) { text = text?.replaceAll("§", " "); } - // Create shadow text with appropriate shadow colors for each color segment - const colorCodeRegex = /\\([a-zA-Z0-9,]+)\\/g; - - function createShadowText(text) { - return text.replace(colorCodeRegex, (match, colorValue) => { - // Determine if this color needs light or dark shadow - let needsLightShadow = false; - - // Check if it's an RGB color (e.g., "255,0,0") - if (colorValue.includes(',')) { - const [r, g, b] = colorValue.split(',').map(n => parseInt(n) || 0); - if (!isNaN(r) && !isNaN(g) && !isNaN(b)) { - // Calculate brightness using standard luminance formula - const brightness = (r * 0.299 + g * 0.587 + b * 0.114); - needsLightShadow = brightness < 64; // Very dark color - } - } - // Check for named dark colors - else if (['black', 'darkred', 'darkblue', 'darkgreen', 'navy', 'maroon', 'darkgray', 'darkgrey', 'dimgray', 'dimgrey', 'darkslategray', 'darkslategrey'].includes(colorValue.toLowerCase())) { - needsLightShadow = true; - } - - // Return appropriate shadow color code - return needsLightShadow ? '\\192,192,192\\' : '\\0,0,0\\'; - }); - } - - const shadowText = createShadowText(text); - - // Tiny hud label option - set this to true to use MatrixChunky8 font for corner labels - const useTinyHudLabel = false; // Change to true to enable tiny pixel font for HUD labels - - // Only check MatrixChunky8 font loading if tiny hud label is requested - let renderText = true; - - if (useTinyHudLabel) { - const cornerFont = typefaceCache.get("MatrixChunky8"); - renderText = false; - - if (cornerFont && cornerFont.glyphs) { - // Check if font has valid glyphs for common characters used in corner labels - const testChars = ['a', 'A', '0', 'r', 'o', 'z', 'e']; - let validGlyphCount = 0; - - for (const char of testChars) { - const glyph = cornerFont.glyphs[char]; - if (glyph && (glyph.pixels || glyph.commands || glyph.resolution)) { - validGlyphCount++; - } - } - - // Only render if we have most of the common glyphs loaded - renderText = validGlyphCount >= 5; - } - } - - if (renderText) { - // SIMPLIFIED: Simple positioning without complex highlight mode offsets - $.ink([0, 0, 0]).write( // Default ink for shadow rendering (color codes in shadowText will override) - shadowText, - { x: 1 + currentHUDScrub, y: 1 }, - undefined, - $api.screen.width - $api.typeface.blockWidth, - false, - useTinyHudLabel ? "MatrixChunky8" : undefined - ); - $.ink(c).write( - text, - { x: 0 + currentHUDScrub, y: 0 }, - undefined, - $api.screen.width - $api.typeface.blockWidth, - false, - useTinyHudLabel ? "MatrixChunky8" : undefined - ); + const hudTextX = HUD_LABEL_TEXT_MARGIN + currentHUDScrub; + const hudTextY = 0; + const typefaceNameForWrite = selectedHudFont; + const hasColorCodes = textContainsColorCodes(text); + const baseTextColor = currentHUDTextColor || "white"; + + drawHudLabelText($, text, { + x: hudTextX, + y: hudTextY, + typefaceName: typefaceNameForWrite, + textColor: baseTextColor, + preserveColors: hasColorCodes, + }); - if (currentHUDScrub > 0) { - const shareWidth = tf.blockWidth * "share ".length; - const shadowShareText = "share"; // No color codes in "share" - $.ink(0).write(shadowShareText, { - x: 1 + currentHUDScrub - shareWidth, - y: 1, - }, undefined, undefined, false, useTinyHudLabel ? "MatrixChunky8" : undefined); - $.ink(c).write("share", { - x: 0 + currentHUDScrub - shareWidth, - y: 0, - }, undefined, undefined, false, useTinyHudLabel ? "MatrixChunky8" : undefined); - } + if (currentHUDScrub > 0) { + const shareWidth = currentHUDShareWidth || (currentHUDLabelBlockWidth * "share ".length); + const shareTextX = HUD_LABEL_TEXT_MARGIN + currentHUDScrub - shareWidth; + const shareTextY = 0; + + drawHudLabelText($, "share", { + x: shareTextX, + y: shareTextY, + typefaceName: typefaceNameForWrite, + textColor: c, + preserveColors: false, + }); } } else { $.ink(0).line(1, 1, 1, h - 1); @@ -9840,15 +10177,18 @@ async function makeFrame({ data: { type, content } }) { // Attach a label buffer if necessary. // Hide label when QR is in fullscreen mode if (label && !hudAnimationState.qrFullscreen) { + const finalX = currentHUDOffset.x + hudAnimationState.slideOffset.x; + const finalY = currentHUDOffset.y + hudAnimationState.slideOffset.y; + sendData.label = { - x: currentHUDOffset.x + hudAnimationState.slideOffset.x, - y: currentHUDOffset.y + hudAnimationState.slideOffset.y, + x: finalX, + y: finalY, opacity: hudAnimationState.opacity, img: (({ width, height, pixels }) => ({ width, height, pixels }))( label, ), }; - + // DEBUG: Add hitbox visualization overlay if (globalThis.debugHudHitbox && currentHUDButton) { const hitboxWidth = currentHUDButton.box.w; @@ -9893,6 +10233,7 @@ async function makeFrame({ data: { type, content } }) { // Use the centralized KidLisp detection that includes comma syntax (sourceCode && lisp.isKidlispSource(sourceCode)); + if (isInlineKidlispPiece && sourceCode && !hideLabel && (hudAnimationState.visible || hudAnimationState.animating || hudAnimationState.qrFullscreen)) { try { // For $code pieces, the sourceCode is the code itself, so use it directly @@ -9918,6 +10259,7 @@ async function makeFrame({ data: { type, content } }) { // Only show QR if MatrixChunky8 font is available const shouldShowQR = !!font; + // Check if this QR overlay is already cached (unless caching is disabled or font not loaded) const isQRCacheDisabled = isQROverlayCacheDisabled(); const hasQRCache = qrOverlayCache.has(cacheKey); @@ -9928,7 +10270,9 @@ async function makeFrame({ data: { type, content } }) { // Declare variables for QR positioning and sizing (used in both cached and fresh QR paths) let overlayWidth, overlayHeight, startX, startY; + if (!isQRCacheDisabled && shouldShowQR && hasQRCache && !forceCacheBypass) { + const cachedQrData = qrOverlayCache.get(cacheKey); // Store QR dimensions in animation state for proper bounding box animations @@ -10000,10 +10344,24 @@ async function makeFrame({ data: { type, content } }) { } } - // Temporarily disable text rendering to prevent ? characters - // TODO: Fix glyph loading pipeline to avoid fallback question marks - console.log("🔤 QR text rendering temporarily disabled to prevent ? characters"); - // The rest of the text rendering code is commented out until the glyph loading is fixed + // Add styled code text positioned at bottom-left corner of QR with padding + const textX = qrOffsetX + 4; // Add 4px left padding from QR edge + const textY = overlayHeight + 4; // Add 2px more gap (was 2, now 4) for extra top padding + + // Draw white text on black background - try MatrixChunky8 first, fallback to default + $.ink("white"); + const matrixFont = typefaceCache.get("MatrixChunky8"); + if (matrixFont && matrixFont.glyphs) { + try { + $.write(codeText, { x: textX, y: textY, size: 2 }, "black", undefined, false, "MatrixChunky8"); + } catch (error) { + // Fallback to default font if MatrixChunky8 fails + $.write(codeText, { x: textX, y: textY, size: 2 }, "black"); + } + } else { + // Use default font if MatrixChunky8 not available + $.write(codeText, { x: textX, y: textY, size: 2 }, "black"); + } }); } else { // Normal mode: use original positioning @@ -10028,7 +10386,9 @@ async function makeFrame({ data: { type, content } }) { opacity: hudAnimationState.opacity, img: qrOverlay }; + } else { + // Always use prompt.ac for QR codes (even in dev/local) let url = "https://prompt.ac"; @@ -10040,7 +10400,7 @@ async function makeFrame({ data: { type, content } }) { // Calculate size and position for bottom-right corner with 4px margin const margin = 4; - const cellSize = 1; // 1 pixel per cell for smallest possible size + const cellSize = 1; // 1 pixel per cell for smallest 1:1 size const qrSize = cells.length * cellSize; // Store QR dimensions in animation state for proper bounding box animations @@ -10179,17 +10539,17 @@ async function makeFrame({ data: { type, content } }) { $.write(codeToRender, { x: textX, y: textY }); } } else { - // Use MatrixChunky8 font in normal mode - with fallback + // Use MatrixChunky8 font in normal mode - with simple fallback const matrixFont = typefaceCache.get("MatrixChunky8"); - if (matrixFont && matrixFont.glyphs) { - const testChar = matrixFont.glyphs['$'] || matrixFont.glyphs['A'] || matrixFont.glyphs['a']; - const hasValidGlyphs = testChar && (testChar.pixels || testChar.commands || testChar.resolution); - if (hasValidGlyphs) { + if (matrixFont) { + try { $.write(codeToRender, { x: textX, y: textY }, undefined, undefined, false, "MatrixChunky8"); - } else { + } catch (error) { + // Fallback to default font if MatrixChunky8 fails $.write(codeToRender, { x: textX, y: textY }); } } else { + // Use default font if MatrixChunky8 not available $.write(codeToRender, { x: textX, y: textY }); } } @@ -10213,20 +10573,15 @@ async function makeFrame({ data: { type, content } }) { $.write(codeToRender, { x: textX + 1, y: textY + 1 }); } } else { - try { - const matrixFont = typefaceCache.get("MatrixChunky8"); - if (matrixFont && matrixFont.glyphs) { - const testChar = matrixFont.glyphs['$'] || matrixFont.glyphs['A'] || matrixFont.glyphs['a']; - const hasValidGlyphs = testChar && (testChar.pixels || testChar.commands || testChar.resolution); - if (hasValidGlyphs) { - $.write(codeToRender, { x: textX + 1, y: textY + 1 }, undefined, undefined, false, "MatrixChunky8"); - } else { - $.write(codeToRender, { x: textX + 1, y: textY + 1 }); - } - } else { + // Try MatrixChunky8 with simple fallback + const matrixFont = typefaceCache.get("MatrixChunky8"); + if (matrixFont) { + try { + $.write(codeToRender, { x: textX + 1, y: textY + 1 }, undefined, undefined, false, "MatrixChunky8"); + } catch (error) { $.write(codeToRender, { x: textX + 1, y: textY + 1 }); } - } catch (error) { + } else { $.write(codeToRender, { x: textX + 1, y: textY + 1 }); } } @@ -10248,20 +10603,15 @@ async function makeFrame({ data: { type, content } }) { $.write(codeToRender, { x: textX, y: textY }); } } else { - try { - const matrixFont = typefaceCache.get("MatrixChunky8"); - if (matrixFont && matrixFont.glyphs) { - const testChar = matrixFont.glyphs['$'] || matrixFont.glyphs['A'] || matrixFont.glyphs['a']; - const hasValidGlyphs = testChar && (testChar.pixels || testChar.commands || testChar.resolution); - if (hasValidGlyphs) { - $.write(codeToRender, { x: textX, y: textY }, undefined, undefined, false, "MatrixChunky8"); - } else { - $.write(codeToRender, { x: textX, y: textY }); - } - } else { + // Try MatrixChunky8 with simple fallback + const matrixFont = typefaceCache.get("MatrixChunky8"); + if (matrixFont) { + try { + $.write(codeToRender, { x: textX, y: textY }, undefined, undefined, false, "MatrixChunky8"); + } catch (error) { $.write(codeToRender, { x: textX, y: textY }); } - } catch (error) { + } else { $.write(codeToRender, { x: textX, y: textY }); } } @@ -10399,6 +10749,7 @@ async function makeFrame({ data: { type, content } }) { opacity: hudAnimationState.opacity, img: qrOverlay }; + } } else { // No cached code yet - QR will appear once caching is complete diff --git a/system/public/aesthetic.computer/lib/graph.mjs b/system/public/aesthetic.computer/lib/graph.mjs index 5a8ff9e77b..c2a197017a 100644 --- a/system/public/aesthetic.computer/lib/graph.mjs +++ b/system/public/aesthetic.computer/lib/graph.mjs @@ -111,6 +111,14 @@ export function setDebug(newDebug) { debug = newDebug; } +let matrixChunkyDebugCount = 0; + +function matrixDebugEnabled() { + if (typeof window !== "undefined" && window?.acMatrixDebug) return true; + if (typeof globalThis !== "undefined" && globalThis?.acMatrixDebug) return true; + return false; +} + // 🎨 FADE HELPERS: New local fade detection and parsing // These replace the global fade state management system @@ -1216,14 +1224,28 @@ function plot(x, y) { const alpha = c[3]; + // Check if current color is a fade color and resolve it + let plotColor = c; + if (typeof c[0] === 'string' && c[0].startsWith('fade:')) { + // Parse the fade string and get the color at this pixel position + const fadeInfo = parseLocalFade(c[0]); + if (fadeInfo) { + const resolvedColor = getLocalFadeColor(null, x, y, fadeInfo); + plotColor = [...resolvedColor, c[1] || 255]; // Use fade alpha + } else { + // Fallback to solid color if fade parsing fails + plotColor = [255, 0, 255, alpha]; // Magenta to indicate error + } + } + // Erasing if (c[0] === -1 && c[1] === -1 && c[2] === -1) { erase(pixels, i, 1 - c[3] / 255); } else if (alpha === 255) { // No alpha blending, just copy. - pixels.set(c, i); + pixels.set(plotColor, i); } else if (alpha !== 0) { - blend(pixels, c, 0, i); + blend(pixels, plotColor, 0, i); } } @@ -1268,6 +1290,8 @@ function point(...args) { y = randInt(height); } + + // TODO: Add support for {x, y} single argument. 2022.02.02.20.39 x += panTranslation.x; y += panTranslation.y; @@ -3477,7 +3501,48 @@ function draw() { if (pixelRow[col] === 1) { // Only render "on" pixels const pixelX = x + (col * scale); const pixelY = y + (row * scale); - + const isMatrixChunky = drawing?.fontName === "MatrixChunky8"; + + const matrixDebugActive = matrixDebugEnabled(); + if (!matrixDebugActive && matrixChunkyDebugCount !== 0) + matrixChunkyDebugCount = 0; + + if ( + isMatrixChunky && + matrixDebugActive && + row < 8 && + col < 8 + ) { + console.log("🧱 MatrixChunky8 draw", { + char: drawing?.char || drawing?.code || "?", + row, + col, + pixelX, + pixelY, + currentColor: c.slice(0, 4), + skip: skips.length, + mask: activeMask + ? { + x: activeMask.x, + y: activeMask.y, + width: activeMask.width, + height: activeMask.height, + } + : null, + }); + } + + let beforePixel; + let logged = false; + if ( + isMatrixChunky && + matrixDebugActive && + matrixChunkyDebugCount < 20 + ) { + beforePixel = pixel(pixelX, pixelY); + logged = true; + } + // Render pixel as a small rectangle scaled appropriately if (scale === 1) { point(pixelX, pixelY); @@ -3489,6 +3554,21 @@ function draw() { } } } + + if (logged) { + const afterPixel = pixel(pixelX, pixelY); + matrixChunkyDebugCount += 1; + console.log("🪄 MatrixChunky8 point write", { + char: drawing?.char || drawing?.code || "?", + row, + col, + pixelX, + pixelY, + color: c.slice(0, 4), + before: beforePixel, + after: afterPixel, + }); + } } } } @@ -3555,6 +3635,7 @@ function draw() { paintGesture(); } } else if (name === "point") { + thickness === 1 ? point(...args) : circle(args[0], args[1], thickness / 2, true); diff --git a/system/public/aesthetic.computer/lib/kidlisp.mjs b/system/public/aesthetic.computer/lib/kidlisp.mjs index 31d0e9a364..9fec10e654 100644 --- a/system/public/aesthetic.computer/lib/kidlisp.mjs +++ b/system/public/aesthetic.computer/lib/kidlisp.mjs @@ -697,6 +697,10 @@ class KidLisp { this.lastSecondExecutions = {}; // Track last execution times for second-based timing this.instantTriggersExecuted = {}; // Track which instant triggers have already fired + // Isolated random number generator state for each instance + this.randomSeed = Date.now() + Math.random(); // Unique seed per instance + this.randomState = this.randomSeed; + // Cache state (URLs stored per instance) this.shortUrl = null; // Store cached short URL this.cachedCode = null; // Store cached code @@ -2570,11 +2574,11 @@ class KidLisp { const bakedTime = performance.now() - bakedStart; // Then render and update embedded layers - console.log(`🎬 HEADLESS DEBUG: About to call renderEmbeddedLayers with ${this.embeddedLayers?.length || 0} layers`); + const embedStart = performance.now(); this.renderEmbeddedLayers($); const embedTime = performance.now() - embedStart; - console.log(`🎬 HEADLESS DEBUG: renderEmbeddedLayers completed in ${embedTime.toFixed(2)}ms`); + // 🔍 TOTAL FRAME TIMING: Disabled for cleaner console const totalFrameTime = performance.now() - totalFrameStart; @@ -2705,6 +2709,13 @@ class KidLisp { }; } + // Seeded random number generator for isolated state per instance + seededRandom() { + // Simple linear congruential generator for deterministic sequences + this.randomState = (this.randomState * 1664525 + 1013904223) % 4294967296; + return this.randomState / 4294967296; + } + // Main parsing method - handles both single expressions and multi-line input parse(input) { const parseStart = this.startTiming('parse'); @@ -3278,14 +3289,14 @@ class KidLisp { // (random max) - returns 0-max // (random min max) - returns min-max if (args.length === 0) { - return Math.floor(Math.random() * 256); + return Math.floor(this.seededRandom() * 256); } else if (args.length === 1) { const max = args[0]; - return Math.floor(Math.random() * max); + return Math.floor(this.seededRandom() * max); } else if (args.length >= 2) { const min = args[0]; const max = args[1]; - return Math.floor(Math.random() * (max - min + 1)) + min; + return Math.floor(this.seededRandom() * (max - min + 1)) + min; } }, // Check if an image URL is ready to be pasted @@ -3492,9 +3503,9 @@ class KidLisp { if (args.length === 0) { // Called with no arguments - set a random ink color const randomColor = [ - Math.floor(Math.random() * 256), - Math.floor(Math.random() * 256), - Math.floor(Math.random() * 256) + Math.floor(this.seededRandom() * 256), + Math.floor(this.seededRandom() * 256), + Math.floor(this.seededRandom() * 256) ]; this.inkState = randomColor; this.inkStateSet = true; @@ -3674,7 +3685,7 @@ class KidLisp { wiggle: (api, args = []) => { // (wiggle amount) - returns random variation ±amount/2 const amount = args.length > 0 ? args[0] : 10; - return (Math.random() - 0.5) * amount; + return (this.seededRandom() - 0.5) * amount; }, box: (api, args = []) => { // Handle non-array args @@ -3689,16 +3700,16 @@ class KidLisp { // Apply contextual logic based on parameter position switch (index) { case 0: // x coordinate - return Math.floor(Math.random() * (api.screen?.width || 256)); + return Math.floor(this.seededRandom() * (api.screen?.width || 256)); case 1: // y coordinate - return Math.floor(Math.random() * (api.screen?.height || 256)); + return Math.floor(this.seededRandom() * (api.screen?.height || 256)); case 2: // width - return Math.floor(Math.random() * ((api.screen?.width || 256) / 4)) + 10; + return Math.floor(this.seededRandom() * ((api.screen?.width || 256) / 4)) + 10; case 3: // height - return Math.floor(Math.random() * ((api.screen?.height || 256) / 4)) + 10; + return Math.floor(this.seededRandom() * ((api.screen?.height || 256) / 4)) + 10; default: // For other parameters, use a reasonable default range - return Math.floor(Math.random() * 256); + return Math.floor(this.seededRandom() * 256); } } return arg; @@ -3836,7 +3847,7 @@ class KidLisp { [0, 1], // scroll down [0, -1] // scroll up ]; - this.scrollFuzzDirection = directions[Math.floor(Math.random() * directions.length)]; + this.scrollFuzzDirection = directions[Math.floor(this.seededRandom() * directions.length)]; // console.log(`🎲 SCROLL fuzzing: first call, randomly chose direction dx=${this.scrollFuzzDirection[0]}, dy=${this.scrollFuzzDirection[1]}`); } dx = this.scrollFuzzDirection[0]; @@ -4552,39 +4563,69 @@ class KidLisp { }, fps: (api, args) => { if (args.length > 0) { - const targetFps = parseFloat(args[0]); - if (!isNaN(targetFps) && targetFps > 0) { - // Store the target FPS in the KidLisp instance - this.targetFps = targetFps; - // Also store in a global location accessible to the tape system + const parsed = parseFloat(args[0]); + if (!isNaN(parsed)) { + if (parsed > 0) { + const targetFps = parsed; + this.targetFps = targetFps; + if (api && typeof api.fps === "function") { + api.fps(targetFps); + } + if (api && api.system) { + api.system.kidlispFps = targetFps; + } + if (typeof window !== 'undefined') { + window.currentKidlispFps = targetFps; + if (!window.kidlispFpsTimeline) { + window.kidlispFpsTimeline = []; + } + const timestamp = performance.now(); + window.kidlispFpsTimeline.push({ + timestamp, + fps: targetFps, + }); + if (window.currentRecordingOptions) { + window.currentRecordingOptions.kidlispFps = targetFps; + window.currentRecordingOptions.kidlispFpsTimeline = window.kidlispFpsTimeline; + console.log(`🎬 Updated recording options with KidLisp FPS: ${targetFps} at ${timestamp.toFixed(2)}ms`); + } + } + if (this._lastLoggedFps !== targetFps) { + console.log(`🎬 KidLisp FPS set to: ${targetFps} at ${(typeof window !== 'undefined' ? performance.now() : 0).toFixed(2)}ms`); + this._lastLoggedFps = targetFps; + } + return targetFps; + } + + // parsed is zero or negative: treat as reset to default + this.targetFps = null; + if (api && typeof api.fps === "function") { + api.fps(null); + } if (api && api.system) { - api.system.kidlispFps = targetFps; + api.system.kidlispFps = null; } - // Store in window for tape system access if (typeof window !== 'undefined') { - window.currentKidlispFps = targetFps; - - // Initialize FPS timeline if it doesn't exist + window.currentKidlispFps = null; if (!window.kidlispFpsTimeline) { window.kidlispFpsTimeline = []; } - - // Add FPS change to timeline with current timestamp const timestamp = performance.now(); window.kidlispFpsTimeline.push({ - timestamp: timestamp, - fps: targetFps + timestamp, + fps: null, }); - - // Also update recording options if they exist (for backward compatibility) if (window.currentRecordingOptions) { - window.currentRecordingOptions.kidlispFps = targetFps; + window.currentRecordingOptions.kidlispFps = null; window.currentRecordingOptions.kidlispFpsTimeline = window.kidlispFpsTimeline; - console.log(`🎬 Updated recording options with KidLisp FPS: ${targetFps} at ${timestamp.toFixed(2)}ms`); + console.log(`🎬 Reset recording KidLisp FPS override at ${timestamp.toFixed(2)}ms`); } } - console.log(`🎬 KidLisp FPS set to: ${targetFps} at ${(typeof window !== 'undefined' ? performance.now() : 0).toFixed(2)}ms`); - return targetFps; + if (this._lastLoggedFps !== null) { + console.log(`🎬 KidLisp FPS reset to default at ${(typeof window !== 'undefined' ? performance.now() : 0).toFixed(2)}ms`); + this._lastLoggedFps = null; + } + return 60; } } return this.targetFps || 60; // Default to 60 FPS if no fps was set @@ -4816,7 +4857,7 @@ class KidLisp { // Fast color selection if (colorChoices) { - const colorIndex = Math.floor(Math.random() * colorChoices.length); + const colorIndex = Math.floor(this.seededRandom() * colorChoices.length); api.ink?.(colorChoices[colorIndex]); } else { this.evaluate(inkExpr, api, this.localEnv); @@ -4825,7 +4866,7 @@ class KidLisp { // Fast text and position evaluation let text, x, y; if (textChoices) { - const textIndex = Math.floor(Math.random() * textChoices.length); + const textIndex = Math.floor(this.seededRandom() * textChoices.length); text = textChoices[textIndex]; } else { text = this.fastEval(writeExpr[1], api, this.localEnv); @@ -4947,8 +4988,8 @@ class KidLisp { } } - // Fast random selection from cached args - const randomIndex = Math.floor(Math.random() * cachedArgs.length); + // Fast random selection from cached args using isolated random + const randomIndex = Math.floor(this.seededRandom() * cachedArgs.length); return cachedArgs[randomIndex]; } @@ -4956,8 +4997,8 @@ class KidLisp { if (api.help?.choose) { return api.help.choose(...args); } - // Fallback to simple random selection - const randomIndex = Math.floor(Math.random() * args.length); + // Fallback to simple random selection using isolated random + const randomIndex = Math.floor(this.seededRandom() * args.length); return args[randomIndex]; }, // 🎲 Random selection (alias) @@ -4968,8 +5009,8 @@ class KidLisp { if (api.help?.choose) { return api.help.choose(...args); } - // Fallback to simple random selection from arguments - const randomIndex = Math.floor(Math.random() * args.length); + // Fallback to simple random selection from arguments using isolated random + const randomIndex = Math.floor(this.seededRandom() * args.length); return args[randomIndex]; } @@ -5800,6 +5841,10 @@ class KidLisp { { min: 0, max: 255 }, // blue (arg 2) { min: 0, max: 255 }, // alpha (arg 3) ], + flood: [ + { min: 0, max: width }, // x position (arg 0) + { min: 0, max: height }, // y position (arg 1) + ], embed: [ // arg 0 is the cache code (string), so no range for that { min: 32, max: 512 }, // width (arg 1) @@ -5815,11 +5860,11 @@ class KidLisp { const functionRanges = contextRanges[functionName]; if (functionRanges && functionRanges[argIndex]) { const range = functionRanges[argIndex]; - return Math.floor(Math.random() * (range.max - range.min + 1)) + range.min; + return Math.floor(this.seededRandom() * (range.max - range.min + 1)) + range.min; } // Default range if no specific context is defined - return Math.floor(Math.random() * 256); + return Math.floor(this.seededRandom() * 256); } // Fast evaluation for common expressions to avoid full recursive evaluation @@ -5829,7 +5874,7 @@ class KidLisp { // Handle randomization token if (expr === "?") { // Generate a random number from 0 to 255 (default range) - return Math.floor(Math.random() * 256); + return Math.floor(this.seededRandom() * 256); } // Handle negative identifiers like "-frame", "-width", etc. @@ -7334,7 +7379,7 @@ class KidLisp { // Safety check: ensure color is defined if (!color) { - console.warn(`⚠️ getTokenColor returned undefined for token:`, token, `at index:`, i); + console.warn(`⚠️ getTokenColor returned undefined for token:`, token, `at index:`, i, "surrounding tokens:", tokens[i-1] || "START", "->", token, "->", tokens[i+1] || "END"); continue; // Skip this token if color is undefined } @@ -7863,6 +7908,13 @@ class KidLisp { // If this token is inside a timing expression, color it based on active state if (timingExprState) { + // Exclude parentheses from timing-based color changes to prevent flashing + if (token === "(" || token === ")") { + // Parentheses should use normal syntax highlighting, not timing colors + const normalColor = this.getNormalTokenColor(token, tokens, index); + return normalColor || "192,192,192"; // Default to gray + } + if (timingExprState.isInActiveArg) { // Check if the timing token for this expression is currently blinking const isTimingBlinking = this.isTimingBlinking(timingExprState.timingData.timingToken); @@ -7887,6 +7939,13 @@ class KidLisp { } if (delayTimerState && delayTimerState.isDelayTimer) { + // Exclude parentheses from delay timer color changes to prevent flashing + if (token === "(" || token === ")") { + // Parentheses should use normal syntax highlighting, not timing colors + const normalColor = this.getNormalTokenColor(token, tokens, index); + return normalColor || "192,192,192"; // Default to gray + } + if (delayTimerState.isBlinking) { // console.log(`� Token "${token}" returning MAGENTA (blinking)`); // All contents of delay timer flash red when triggered (brief flash) @@ -8400,14 +8459,34 @@ class KidLisp { embeddedKidLisp.onceExecuted.clear(); console.log(`🔄 CLEARED onceExecuted for re-execution`); - // Reset timing states for fresh execution - embeddedKidLisp.frameCount = 0; - embeddedKidLisp.frameCounter = 0; - embeddedKidLisp.timingStates.clear(); - // IMPORTANT: Share timing state with parent for consistent timer behavior - embeddedKidLisp.lastSecondExecutions = this.lastSecondExecutions; - // IMPORTANT: Share sequence counters for timing expressions - embeddedKidLisp.sequenceCounters = this.sequenceCounters || new Map(); + // CRITICAL: Restore isolated timing state for existing layers + const layerCacheKey = `${source}_timing`; + let existingTimingState = this.embeddedLayerCache?.get?.(layerCacheKey); + + if (existingTimingState) { + // Restore persistent timing state for this specific layer + embeddedKidLisp.frameCount = existingTimingState.frameCount || 0; + embeddedKidLisp.frameCounter = existingTimingState.frameCounter || 0; + embeddedKidLisp.lastSecondExecutions = existingTimingState.lastSecondExecutions || {}; + embeddedKidLisp.sequenceCounters = new Map(existingTimingState.sequenceCounters || []); + embeddedKidLisp.timingStates = new Map(existingTimingState.timingStates || []); + // Restore the random state too for consistency + if (existingTimingState.randomState) { + embeddedKidLisp.randomState = existingTimingState.randomState; + } + } else { + // Fresh reset - start with clean isolated timing state + embeddedKidLisp.frameCount = 0; + embeddedKidLisp.frameCounter = 0; + embeddedKidLisp.lastSecondExecutions = {}; + embeddedKidLisp.sequenceCounters = new Map(); + embeddedKidLisp.timingStates = new Map(); + // Give a unique random seed for existing layers that don't have cached state + if (!embeddedKidLisp.randomSeed) { + embeddedKidLisp.randomSeed = Date.now() + Math.random() + (source.hashCode?.() || 0); + embeddedKidLisp.randomState = embeddedKidLisp.randomSeed; + } + } embeddedKidLisp.localEnv = { ...this.localEnv }; // Ensure it remains marked as a nested instance @@ -8424,14 +8503,35 @@ class KidLisp { // Create new embedded layer const embeddedKidLisp = new KidLisp(); - // Start embedded layers with fresh timing states for re-entrancy - embeddedKidLisp.frameCount = 0; - embeddedKidLisp.frameCounter = 0; - embeddedKidLisp.timingStates = new Map(); - // IMPORTANT: Share timing state with parent for consistent timer behavior - embeddedKidLisp.lastSecondExecutions = this.lastSecondExecutions; - // IMPORTANT: Share sequence counters for timing expressions - embeddedKidLisp.sequenceCounters = this.sequenceCounters || new Map(); + + // CRITICAL: Give each embedded layer a unique random seed for isolated random state + embeddedKidLisp.randomSeed = Date.now() + Math.random() + (source.hashCode?.() || 0); + embeddedKidLisp.randomState = embeddedKidLisp.randomSeed; + + // CRITICAL: Each embedded layer needs ISOLATED but PERSISTENT timing state + // Check if we have cached timing state for this layer + const layerCacheKey = `${source}_timing`; + let existingTimingState = this.embeddedLayerCache?.get?.(layerCacheKey); + + if (existingTimingState) { + // Restore persistent timing state for this specific layer + embeddedKidLisp.frameCount = existingTimingState.frameCount || 0; + embeddedKidLisp.frameCounter = existingTimingState.frameCounter || 0; + embeddedKidLisp.lastSecondExecutions = existingTimingState.lastSecondExecutions || {}; + embeddedKidLisp.sequenceCounters = new Map(existingTimingState.sequenceCounters || []); + embeddedKidLisp.timingStates = new Map(existingTimingState.timingStates || []); + // Restore the random state too for consistency + if (existingTimingState.randomState) { + embeddedKidLisp.randomState = existingTimingState.randomState; + } + } else { + // Fresh layer - start with clean timing state + embeddedKidLisp.frameCount = 0; + embeddedKidLisp.frameCounter = 0; + embeddedKidLisp.lastSecondExecutions = {}; + embeddedKidLisp.sequenceCounters = new Map(); + embeddedKidLisp.timingStates = new Map(); + } embeddedKidLisp.localEnv = { ...this.localEnv }; // Mark this as a nested instance to enable immediate mode for commands like blur/scroll @@ -8486,8 +8586,14 @@ class KidLisp { // Source changed, clear the buffer and create a new one if (persistentLayer.buffer && persistentLayer.buffer.pixels) { - // Fast buffer clear using fill - persistentLayer.buffer.pixels.fill(0); + // Initialize with opaque black (like main canvas) + for (let i = 0; i < persistentLayer.buffer.pixels.length; i += 4) { + persistentLayer.buffer.pixels[i] = 0; // R + persistentLayer.buffer.pixels[i + 1] = 0; // G + persistentLayer.buffer.pixels[i + 2] = 0; // B + persistentLayer.buffer.pixels[i + 3] = 255; // A - opaque! + } + console.log(`🎨 EMBEDDED BUFFER CLEAR: Reinitialized buffer with opaque black [0,0,0,255]`); } embeddedBuffer = persistentLayer.buffer; // Reuse cleared buffer @@ -8510,10 +8616,17 @@ class KidLisp { foundCompatibleBuffer = true; console.log(`🔄 Reusing existing buffer during reframe: ${width}x${height} fits in ${existingLayer.buffer.width}x${existingLayer.buffer.height}`); - // Clear only the used area + // Initialize used area with opaque black if (embeddedBuffer.pixels) { - const totalPixels = width * height * 4; // RGBA - embeddedBuffer.pixels.fill(0, 0, totalPixels); + const totalPixels = width * height; + for (let i = 0; i < totalPixels; i++) { + const pixelIndex = i * 4; + embeddedBuffer.pixels[pixelIndex] = 0; // R + embeddedBuffer.pixels[pixelIndex + 1] = 0; // G + embeddedBuffer.pixels[pixelIndex + 2] = 0; // B + embeddedBuffer.pixels[pixelIndex + 3] = 255; // A - opaque! + } + console.log(`🎨 EMBEDDED BUFFER REFRAME: Initialized used area with opaque black [0,0,0,255]`); } } } @@ -8527,9 +8640,15 @@ class KidLisp { embeddedBuffer = pooledBuffers.pop(); // console.log(`🔄 Reused pooled buffer ${bufferSizeKey} (${pooledBuffers.length} remaining)`); - // Clear the reused buffer + // Initialize the reused buffer with opaque black (like main canvas) if (embeddedBuffer.pixels) { - embeddedBuffer.pixels.fill(0); + for (let i = 0; i < embeddedBuffer.pixels.length; i += 4) { + embeddedBuffer.pixels[i] = 0; // R + embeddedBuffer.pixels[i + 1] = 0; // G + embeddedBuffer.pixels[i + 2] = 0; // B + embeddedBuffer.pixels[i + 3] = 255; // A - opaque! + } + console.log(`🎨 EMBEDDED BUFFER INIT: Initialized buffer with opaque black [0,0,0,255]`); } } else { // Use optimized buffer creation @@ -8772,8 +8891,14 @@ class KidLisp { // 🛡️ SAFETY CHECK: Ensure buffer is not detached if (buffer.pixels && buffer.pixels.buffer && !buffer.pixels.buffer.detached) { - // Buffer is safe to reuse - clear it - buffer.pixels.fill(0); + // Buffer is safe to reuse - initialize with opaque black + for (let i = 0; i < buffer.pixels.length; i += 4) { + buffer.pixels[i] = 0; // R + buffer.pixels[i + 1] = 0; // G + buffer.pixels[i + 2] = 0; // B + buffer.pixels[i + 3] = 255; // A - opaque! + } + console.log(`🎨 BUFFER POOL REUSE: Initialized with opaque black [0,0,0,255]`); return buffer; } else { // Buffer is detached or invalid, remove it and create new one @@ -8781,12 +8906,24 @@ class KidLisp { } } - // Create new buffer with pre-zeroed memory for faster allocation + // Create new buffer initialized with opaque black const pixelCount = width * height * 4; + const pixels = new Uint8ClampedArray(pixelCount); + + // Initialize with opaque black [0,0,0,255] + for (let i = 0; i < pixelCount; i += 4) { + pixels[i] = 0; // R + pixels[i + 1] = 0; // G + pixels[i + 2] = 0; // B + pixels[i + 3] = 255; // A - opaque! + } + + console.log(`🎨 NEW BUFFER CREATED: ${width}x${height} initialized with opaque black [0,0,0,255]`); + return { width: width, height: height, - pixels: new Uint8ClampedArray(pixelCount) // Already zeroed + pixels: pixels }; } @@ -8969,6 +9106,13 @@ class KidLisp { const dstWidth = api.screen.width; const dstHeight = api.screen.height; + // Debug: Check destination buffer before blending + const dstSamplePixels = []; + for (let i = 0; i < Math.min(20, dstPixels.length); i += 4) { + dstSamplePixels.push(`[${dstPixels[i]},${dstPixels[i+1]},${dstPixels[i+2]},${dstPixels[i+3]}]`); + } + console.log(`🎭 BLEND DEBUG: Destination buffer before blending (alpha=${alpha}):`, dstSamplePixels.slice(0, 5).join(', ')); + // Normalize alpha to 0-1 range const alphaFactor = alpha / 255.0; @@ -9034,7 +9178,7 @@ class KidLisp { renderEmbeddedLayers(api) { // 🔍 DEBUG: Log embedded layers status if (!this.embeddedLayers || this.embeddedLayers.length === 0) { - console.log("🎬 HEADLESS DEBUG: No embedded layers to render"); + return; } @@ -9183,8 +9327,9 @@ class KidLisp { } embeddedLayer.localFrameCount += 1; + // CRITICAL: Each embedded layer needs its own independent timeline embeddedLayer.kidlispInstance.frameCount = embeddedLayer.localFrameCount; - embeddedLayer.kidlispInstance.frameCounter = this.frameCounter; + embeddedLayer.kidlispInstance.frameCounter = embeddedLayer.localFrameCount; // Ensure timing state if (!embeddedLayer.kidlispInstance.timingStates) { @@ -9205,7 +9350,9 @@ class KidLisp { localEnv.scroll = frameValue % (embeddedLayer.width + embeddedLayer.height); // Apply fade background only once + console.log(`🎨 EMBEDDED FADE DEBUG: firstLineColor=${embeddedLayer.kidlispInstance.firstLineColor}, fadeApplied=${embeddedLayer.fadeApplied}`); if (embeddedLayer.kidlispInstance.firstLineColor && !embeddedLayer.fadeApplied) { + console.log(`🎨 EMBEDDED FADE: Applying background wipe with ${embeddedLayer.kidlispInstance.firstLineColor}`); embeddedApi.wipe(embeddedLayer.kidlispInstance.firstLineColor); embeddedLayer.fadeApplied = true; } @@ -9247,6 +9394,19 @@ class KidLisp { localEnv ); + // CRITICAL: Save persistent timing state after execution + const layerCacheKey = `${embeddedLayer.source}_timing`; + if (this.embeddedLayerCache) { + this.embeddedLayerCache.set(layerCacheKey, { + frameCount: embeddedLayer.kidlispInstance.frameCount, + frameCounter: embeddedLayer.kidlispInstance.frameCounter, + lastSecondExecutions: [...embeddedLayer.kidlispInstance.lastSecondExecutions], + sequenceCounters: Array.from(embeddedLayer.kidlispInstance.sequenceCounters), + timingStates: Array.from(embeddedLayer.kidlispInstance.timingStates), + randomState: embeddedLayer.kidlispInstance.randomState + }); + } + embeddedLayer.hasBeenEvaluated = true; embeddedLayer.lastFrameEvaluated = frameValue; } @@ -9255,7 +9415,6 @@ class KidLisp { api.page(api.screen); // Paste with optimized alpha compositing - console.log(`🎬 HEADLESS DEBUG: About to pasteWithAlpha buffer ${embeddedLayer.buffer?.width}x${embeddedLayer.buffer?.height} at (${embeddedLayer.x},${embeddedLayer.y})`); this.pasteWithAlpha(api, embeddedLayer.buffer, embeddedLayer.x, embeddedLayer.y, embeddedLayer.alpha); } @@ -9336,7 +9495,10 @@ class KidLisp { }, width: embeddedLayer.width, height: embeddedLayer.height, - frame: 0 // Updated per render + frame: 0, // Updated per render + + // 🚨 CRITICAL: Include clock API for timing expressions in embedded layers + clock: api.clock }; return embeddedApi; @@ -9586,9 +9748,9 @@ class KidLisp { } embeddedLayer.localFrameCount += 1; - // Update the embedded KidLisp instance's frame counter + // CRITICAL: Each embedded layer needs its own independent timeline embeddedLayer.kidlispInstance.frameCount = embeddedLayer.localFrameCount; - embeddedLayer.kidlispInstance.frameCounter = this.frameCounter; + embeddedLayer.kidlispInstance.frameCounter = embeddedLayer.localFrameCount; // Ensure timing state is preserved for the embedded instance if (!embeddedLayer.kidlispInstance.timingStates) { @@ -10106,66 +10268,81 @@ async function fetchCachedCode(nanoidCode, api = null) { return null; // Return null in TEIA mode } - // Always use /api endpoint for store-kidlisp - const fullUrl = `/api/store-kidlisp?code=${nanoidCode}`; - - // Use Node.js https module with SSL bypass like tape.mjs does - return new Promise(async (resolve, reject) => { - // Check if we're in Node.js environment - if (typeof process !== 'undefined' && process.versions && process.versions.node) { - // Import https module dynamically for Node.js environment - const https = await import('https'); - const options = { rejectUnauthorized: false }; // Allow self-signed certificates + // Helper function to try fetching from a specific URL + const tryFetch = async (url, isProduction = false) => { + return new Promise(async (resolve) => { + // Check if we're in Node.js environment + if (typeof process !== 'undefined' && process.versions && process.versions.node) { + // Import https module dynamically for Node.js environment + const https = await import('https'); + const options = { rejectUnauthorized: false }; // Allow self-signed certificates - https.get(fullUrl, options, (res) => { - let data = ''; + https.get(url, options, (res) => { + let data = ''; - res.on('data', (chunk) => { - data += chunk; - }); + res.on('data', (chunk) => { + data += chunk; + }); - res.on('end', () => { - try { - const parsed = JSON.parse(data); - if (parsed && parsed.source) { - console.log(`✅ Successfully fetched KidLisp code: ${nanoidCode} (${parsed.source.length} chars)`); - resolve(parsed.source); - } else { - console.error(`❌ Failed to load cached code: ${nanoidCode} - No source in response`, parsed); + res.on('end', () => { + try { + const parsed = JSON.parse(data); + if (parsed && parsed.source) { + console.log(`✅ Successfully fetched KidLisp code: ${nanoidCode} from ${isProduction ? 'production' : 'local'} (${parsed.source.length} chars)`); + resolve(parsed.source); + } else { + console.error(`❌ Failed to load cached code: ${nanoidCode} - No source in response from ${url}`, parsed); + resolve(null); + } + } catch (err) { + console.error(`❌ Failed to parse JSON for cached code: ${nanoidCode} from ${url}`, err, 'Raw response:', data); resolve(null); } - } catch (err) { - console.error(`❌ Failed to parse JSON for cached code: ${nanoidCode}`, err, 'Raw response:', data); + }); + }).on('error', (err) => { + console.error(`❌ Network error loading cached code: ${nanoidCode} from ${url}`, err); + resolve(null); + }); + } else { + // Browser environment - fallback to fetch + fetch(url).then(response => { + if (response.ok) { + return response.json().then(data => { + if (data && data.source) { + console.log(`✅ Successfully fetched KidLisp code: ${nanoidCode} from ${isProduction ? 'production' : 'local'} (${data.source.length} chars)`); + resolve(data.source); + } else { + console.error(`❌ Failed to load cached code: ${nanoidCode} - No source in response from ${url}`, data); + resolve(null); + } + }); + } else { + console.error(`❌ Failed to load cached code: ${nanoidCode} - HTTP ${response.status}: ${response.statusText} from ${url}`); resolve(null); } - }); - }).on('error', (err) => { - console.error(`❌ Network error loading cached code: ${nanoidCode}`, err); - resolve(null); - }); - } else { - // Browser environment - fallback to fetch - fetch(fullUrl).then(response => { - if (response.ok) { - return response.json().then(data => { - if (data && data.source) { - // console.log(`✅ Successfully fetched KidLisp code: ${nanoidCode} (${data.source.length} chars)`); - resolve(data.source); - } else { - console.error(`❌ Failed to load cached code: ${nanoidCode} - No source in response`, data); - resolve(null); - } - }); - } else { - console.error(`❌ Failed to load cached code: ${nanoidCode} - HTTP ${response.status}: ${response.statusText}`); + }).catch(error => { + console.error(`❌ Network error loading cached code: ${nanoidCode} from ${url}`, error); resolve(null); - } - }).catch(error => { - console.error(`❌ Network error loading cached code: ${nanoidCode} from ${fullUrl}`, error); - resolve(null); - }); - } - }); + }); + } + }); + }; + + // First try local dev server + const localUrl = `/api/store-kidlisp?code=${nanoidCode}`; + console.log(`🔍 Attempting to fetch ${nanoidCode} from local dev server: ${localUrl}`); + const localSource = await tryFetch(localUrl, false); + + if (localSource) { + return localSource; + } + + // Fallback to production aesthetic.computer domain + const productionUrl = `https://aesthetic.computer/api/store-kidlisp?code=${nanoidCode}`; + console.log(`🔄 Local dev server failed, trying production fallback: ${productionUrl}`); + const productionSource = await tryFetch(productionUrl, true); + + return productionSource; } // Export function to get syntax highlighting colors for progress bars diff --git a/system/public/aesthetic.computer/lib/type.mjs b/system/public/aesthetic.computer/lib/type.mjs index 29f37c9cee..94a0b1531a 100644 --- a/system/public/aesthetic.computer/lib/type.mjs +++ b/system/public/aesthetic.computer/lib/type.mjs @@ -9,6 +9,11 @@ endregion */ import * as fonts from "../disks/common/fonts.mjs"; import { repeat } from "../lib/help.mjs"; import { checkTeiaMode } from "./teia-mode.mjs"; +function matrixDebugEnabled() { + if (typeof window !== "undefined" && window?.acMatrixDebug) return true; + if (typeof globalThis !== "undefined" && globalThis?.acMatrixDebug) return true; + return false; +} const { floor, min } = Math; const { keys, entries } = Object; @@ -19,6 +24,7 @@ class Typeface { data; name; glyphs = {}; + advanceCache = new Map(); //loaded = false; constructor(name = "font_1") { @@ -32,10 +38,101 @@ class Typeface { bdfFont: "MatrixChunky8", proportional: true, advances: { - 'A': 4, 'B': 4, 'C': 4, 'D': 4, 'E': 4, 'F': 4, 'G': 4, 'H': 5, 'I': 4, 'J': 4, 'K': 4, 'L': 4, 'M': 6, 'N': 5, 'O': 5, 'P': 4, 'Q': 4, 'R': 4, 'S': 4, 'T': 4, 'U': 4, 'V': 4, 'W': 6, 'X': 4, 'Y': 4, 'Z': 4, - 'a': 4, 'b': 4, 'c': 4, 'd': 4, 'e': 4, 'f': 4, 'g': 4, 'h': 4, 'i': 4, 'j': 4, 'k': 4, 'l': 4, 'm': 6, 'n': 4, 'o': 4, 'p': 4, 'q': 4, 'r': 4, 's': 4, 't': 4, 'u': 4, 'v': 4, 'w': 6, 'x': 4, 'y': 4, 'z': 4, - '0': 4, '1': 4, '2': 4, '3': 4, '4': 4, '5': 4, '6': 4, '7': 4, '8': 4, '9': 4, - ' ': 2, '.': 2, ',': 2, '!': 2, '?': 4, ':': 2, ';': 2, '-': 4, '+': 4, '=': 4, '<': 4, '>': 4, '/': 4, '\\': 4, '|': 2, '"': 4, "'": 2, '(': 4, ')': 4, '[': 4, ']': 2, '{': 2, '}': 2, '@': 5, '#': 5, '$': 4, '%': 5, '^': 4, '&': 5, '*': 4, '_': 4, '~': 5 + ' ': 2, + '!': 2, + '"': 4, + '#': 8, + '$': 4, + '%': 4, + '&': 5, + '\'': 2, + '(': 7, + ')': 4, + '*': 5, + '+': 4, + ',': 2, + '-': 4, + '.': 2, + '/': 4, + '0': 4, + '1': 4, + '2': 4, + '3': 4, + '4': 4, + '5': 4, + '6': 4, + '7': 4, + '8': 4, + '9': 4, + ':': 2, + ';': 2, + '<': 4, + '=': 4, + '>': 4, + '?': 4, + '@': 5, + 'A': 4, + 'B': 4, + 'C': 4, + 'D': 4, + 'E': 4, + 'F': 4, + 'G': 5, + 'H': 5, + 'I': 4, + 'J': 4, + 'K': 4, + 'L': 4, + 'M': 6, + 'N': 5, + 'O': 5, + 'P': 4, + 'Q': 5, + 'R': 4, + 'S': 4, + 'T': 4, + 'U': 4, + 'V': 4, + 'W': 6, + 'X': 4, + 'Y': 4, + 'Z': 4, + '[': 4, + "\\": 4, + ']': 4, + '^': 4, + '_': 4, + '`': 3, + 'a': 4, + 'b': 4, + 'c': 4, + 'd': 4, + 'e': 4, + 'f': 4, + 'g': 4, + 'h': 4, + 'i': 4, + 'j': 4, + 'k': 4, + 'l': 4, + 'm': 6, + 'n': 4, + 'o': 4, + 'p': 4, + 'q': 4, + 'r': 4, + 's': 4, + 't': 4, + 'u': 4, + 'v': 4, + 'w': 6, + 'x': 4, + 'y': 4, + 'z': 4, + '{': 4, + '|': 2, + '}': 4, + '~': 4 }, bdfOverrides: { 'y': { y: 2 } @@ -147,6 +244,7 @@ class Typeface { } const glyphData = await response.json(); this.data[char] = glyphData; // Store in font data for proxy access + this.invalidateAdvance(char); return glyphData; } catch (err) { return null; @@ -322,6 +420,7 @@ class Typeface { .then((glyphData) => { // Store the loaded glyph target[char] = glyphData; + this.invalidateAdvance(char); // Trigger a re-render if this is for QR text if (this.name === "MatrixChunky8") { @@ -428,6 +527,58 @@ class Typeface { getGlyph(char) { return this.glyphs[char]; } + + getAdvance(char) { + if (!char) return this.blockWidth || 4; + if (this.advanceCache.has(char)) { + return this.advanceCache.get(char); + } + + let advance; + + const glyph = this.glyphs?.[char]; + if (glyph && typeof glyph.advance === "number") { + advance = glyph.advance; + } + + if (advance === undefined) { + const glyphData = this.data?.[char]; + if (glyphData && typeof glyphData.advance === "number") { + advance = glyphData.advance; + } + } + + if (advance === undefined && this.data?.advances) { + advance = this.data.advances[char]; + } + + if (advance === undefined && glyph?.dwidth?.x) { + advance = glyph.dwidth.x; + } + + if (advance === undefined && glyph?.resolution?.[0]) { + advance = glyph.resolution[0]; + } + + if (advance === undefined && typeof this.data?.glyphWidth === "number") { + advance = this.data.glyphWidth; + } + + if (advance === undefined) { + advance = this.blockWidth || 4; + } + + this.advanceCache.set(char, advance); + return advance; + } + + invalidateAdvance(char) { + if (!char) { + this.advanceCache.clear?.(); + } else { + this.advanceCache.delete?.(char); + } + } // 📓 tf.print print( @@ -442,8 +593,10 @@ class Typeface { const font = this.glyphs; const size = pos.size || 1; const blockMargin = 1; + const inferredBlockWidth = + this.data?.glyphWidth ?? this.blockWidth ?? this.data?.width ?? 6; const blockHeight = ((this.blockHeight || 10) + blockMargin) * size; - const blockWidth = this.data.glyphWidth; // * size; + const blockWidth = inferredBlockWidth; const thickness = pos.thickness || 1; const rotation = pos.rotation || 0; const fullWidth = blockWidth * size * text.length; @@ -466,16 +619,15 @@ class Typeface { this.name === "MatrixChunky8"; if (isProportional) { - // For proportional fonts, use character advance widths from font definition - const advances = this.data?.advances || {}; - w = 0; - [...text.toString()].forEach((char) => { - const glyph = this.getGlyph(char); - const glyphAdvance = glyph?.advance; // Check individual glyph advance first - const charAdvance = glyphAdvance || advances[char] || blockWidth; + const chars = [...text.toString()]; + for (const char of chars) { + if (char === "\n") { + continue; + } + const charAdvance = this.getAdvance(char) || inferredBlockWidth; w += charAdvance * size; - }); + } } else { // For monospace fonts, use the original calculation w = text.length * blockWidth * size; @@ -556,11 +708,8 @@ class Typeface { if (typeof charColor === 'object' && charColor.foreground !== undefined && charColor.background !== undefined) { // Draw background first if (charColor.background) { - const glyph = this.getGlyph(char); - const glyphAdvance = glyph?.advance; // Check individual glyph advance first - const charWidth = isProportional ? - (glyphAdvance || this.data?.advances?.[char] || blockWidth) * size : - blockWidth * size; + const charAdvance = this.getAdvance(char); + const charWidth = charAdvance * size; if (Array.isArray(charColor.background)) { $.ink(...charColor.background); } else { @@ -603,17 +752,8 @@ class Typeface { ); // Move to next character position using proper advance width - if (isProportional) { - // Use character-specific advance width for proportional fonts - const glyph = this.getGlyph(char); - const glyphAdvance = glyph?.advance; // Check individual glyph advance first - const advances = this.data?.advances || {}; - const charAdvance = glyphAdvance || advances[char] || blockWidth; - currentX += charAdvance * size; - } else { - // Use fixed width for monospace fonts - currentX += blockWidth * size; - } + const charAdvance = this.getAdvance(char); + currentX += charAdvance * size; } } else { // Original single-color rendering -- knotmirror 0.0.1 From d61b014fe7ab99acb03eee1f3f1185f3cc25a6c0 Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Sun, 28 Sep 2025 05:05:23 +0000 Subject: [PATCH 4/7] rm logs --- system/public/aesthetic.computer/bios.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/public/aesthetic.computer/bios.mjs b/system/public/aesthetic.computer/bios.mjs index be502ef094..9413178842 100644 --- a/system/public/aesthetic.computer/bios.mjs +++ b/system/public/aesthetic.computer/bios.mjs @@ -920,10 +920,10 @@ async function boot(parsed, bpm = 60, resolution, debug) { let UDP; async function loadUDP() { if (!UDP) { - if (debug) console.log("🌐 Loading UDP networking library..."); + // if (debug) console.log("🌐 Loading UDP networking library..."); const udpModule = await import('./lib/udp.mjs'); UDP = udpModule.UDP; - if (debug) console.log("🌐 UDP Ready..."); + // if (debug) console.log("🌐 UDP Ready..."); } return UDP; } -- knotmirror 0.0.1 From 28ba434ec80ad02e627fdcdeaac98ed3f927e03a Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Sun, 28 Sep 2025 09:30:53 +0000 Subject: [PATCH 5/7] chat hotfix (risky) --- .../public/aesthetic.computer/disks/chat.mjs | 357 +++++------------- system/public/aesthetic.computer/lib/disk.mjs | 26 +- 2 files changed, 117 insertions(+), 266 deletions(-) diff --git a/system/public/aesthetic.computer/disks/chat.mjs b/system/public/aesthetic.computer/disks/chat.mjs index 3deff6fce0..0fb7edd01d 100644 --- a/system/public/aesthetic.computer/disks/chat.mjs +++ b/system/public/aesthetic.computer/disks/chat.mjs @@ -292,37 +292,14 @@ function paint( // 🪧 Paint the message and its contents. ink("white", layout.inBox ? 64 : 32).box(x, y, tb.box.width, tb.box.height); + + // Render the color-coded message using the modern color system ink(layout.msgColor).write( layout.paintableMessage, { x, y }, undefined, screen.width - x, ); - layout.handles.forEach((handle) => { - ink(handle.over ? "yellow" : handle.color).write(handle.word, { - x: x + handle.x, - y: handle.y, - }); - }); - layout.prompts.forEach((prompt) => { - // if (handle.over) handle.color = pen?.drawing ? "yellow" : [190, 180, 255]; - prompt.lines.forEach((line) => { - ink(prompt.over ? "yellow" : "lime").write(line.text, { - x: x + line.x, - y: line.y, - }); - }); - }); - - layout.urls.forEach((url) => { - // if (handle.over) handle.color = pen?.drawing ? "yellow" : [190, 180, 255]; - url.lines.forEach((line) => { - ink(url.over ? "yellow" : "orange").write(line.text, { - x: x + line.x, - y: line.y, - }); - }); - }); const ago = timeAgo(message.when); let overTimestamp = false; @@ -516,80 +493,20 @@ function act( message.layout.inBox = true; // 📆 `timestamp` hover and activate. - // (Will always only be on one line) - // for (let h = 0; h < message.layout.timestamp.length; h += 1) { - // const handle = message.layout.handles[h]; - { - const timestamp = message.layout.timestamp; - const startX = message.layout.x + timestamp.x; - if ( - e.x > startX && - e.x < startX + timestamp.width && - e.y > timestamp.y && - e.y < timestamp.y + timestamp.height - ) { - timestamp.over = true; - break; - } - } - // } - - // 👱 `handle` hover and activate. - // (Will always only be on one line) - for (let h = 0; h < message.layout.handles.length; h += 1) { - const handle = message.layout.handles[h]; - const startX = message.layout.x + handle.x; - - if ( - e.x > startX && - e.x < startX + handle.width && - e.y > handle.y && - e.y < handle.y + handle.height - ) { - handle.over = true; - break; - } - } - - // 🖥️ `prompt` hover and activate. - // (With line break support) - for (let p = 0; p < message.layout.prompts.length; p += 1) { - const prompt = message.layout.prompts[p]; - for (let l = 0; l < prompt.lines.length; l += 1) { - const line = prompt.lines[l]; - const startX = message.layout.x + line.x; - - if ( - e.x > startX && - e.x < startX + line.width && - e.y > line.y && - e.y < line.y + line.height - ) { - prompt.over = true; - break; - } - } + const timestamp = message.layout.timestamp; + const startX = message.layout.x + timestamp.x; + if ( + e.x > startX && + e.x < startX + timestamp.width && + e.y > timestamp.y && + e.y < timestamp.y + timestamp.height + ) { + timestamp.over = true; + break; } - // 🕸️ `url` hover and activate. - // (With line break support) - for (let u = 0; u < message.layout.urls.length; u += 1) { - const url = message.layout.urls[u]; - for (let l = 0; l < url.lines.length; l += 1) { - const line = url.lines[l]; - const startX = message.layout.x + line.x; - - if ( - e.x > startX && - e.x < startX + line.width && - e.y > line.y && - e.y < line.y + line.height - ) { - url.over = true; - break; - } - } - } + // Store the clicked message for potential interaction + message.clicked = true; // TODO: ⏰ Add timestamp hover and activate per message. // - With 'delete' action. @@ -603,43 +520,51 @@ function act( if (!message.tb || !message.layout) { continue; // If `tb` is not defined then kill this. 👾 } - // 🟠 TODO: Reset all the other potential tapped handles and prompts. - message.layout.inBox = false; // Handles - for (let h = 0; h < message.layout.handles.length; h += 1) { - const handle = message.layout.handles[h]; - if (handle.over) { - beep(); - hud.labelBack(); - jump(handle.word); - handle.over = false; - break; - } - } // Prompts - for (let p = 0; p < message.layout.prompts.length; p += 1) { - const prompt = message.layout.prompts[p]; - if (prompt.over) { - beep(); - hud.labelBack(); - const innerPrompt = prompt.text.slice(1, -1); // Unquote prompt text. - if (innerPrompt.startsWith(">")) { - jump("prompt " + innerPrompt.slice(1)); - } else { - jump(innerPrompt); + message.layout.inBox = false; + + // Handle clicks on interactive elements with proper hit detection + if (message.clicked) { + message.clicked = false; + + // Parse the original message to find interactive elements + const parsedElements = parseMessageElements(message.fullMessage); + + // Calculate click position relative to message + const relativeX = e.x - message.layout.x; + const relativeY = e.y - message.layout.y; + + // Check each interactive element for hit detection + for (const element of parsedElements) { + // Calculate the position of this element in the rendered text + const elementPosition = calculateElementPosition( + element, + message.fullMessage, + message.tb.lines, + text + ); + + if (elementPosition && isClickInsideElement(relativeX, relativeY, elementPosition)) { + if (element.type === "handle") { + beep(); + hud.labelBack(); + jump(element.text); + break; + } else if (element.type === "prompt") { + beep(); + hud.labelBack(); + const innerPrompt = element.text.slice(1, -1); // Unquote prompt text. + if (innerPrompt.startsWith(">")) { + jump("prompt " + innerPrompt.slice(1)); + } else { + jump(innerPrompt); + } + break; + } else if (element.type === "url") { + beep(); + jump("out:" + element.text); + break; + } } - prompt.over = false; - break; - } - } - - // URLs - for (let u = 0; u < message.layout.urls.length; u += 1) { - const url = message.layout.urls[u]; - if (url.over) { - beep(); - jump("out:" + url.text); - // console.log("🟨 Visiting...", url); - url.over = false; - break; } } @@ -852,137 +777,38 @@ function computeMessagesLayout({ screen, text }, chat) { y -= rowHeight; continue; } - // Outline a 🪧 renderable message and its geometry. - let msgColor = "white", - inBox = msg.lastLayout?.inBox || false; // A flag that determines if we are in the message box or not. - - const handles = []; // Parse '@handle' names. - const prompts = []; // Parse `prompts`. - const urls = []; // Parse any 'urls'. - const paintableMessage = msg.fullMessage; - // Debug: Log message formats (can be removed once mapping is confirmed stable) - // console.log("🔍 Message formats:"); - // console.log(" msg.text (original):", msg.text); - // console.log(" msg.fullMessage (with handle):", msg.fullMessage); - // console.log(" paintableMessage:", paintableMessage); - - // Parse elements from the original message - const parsedElements = parseMessageElements(paintableMessage); - const activeContainers = new Map(); // Track active element containers// Build a more accurate mapping of words to their positions in the original message - const wordToPositionMap = buildWordPositionMap(paintableMessage, msg.tb.lines); - - for (let rowIndex = 0; rowIndex < msg.tb.lines.length; rowIndex += 1) { - let rowY = y + rowIndex * rowHeight; - let cursorX = 0; - - for ( - let wordIndex = 0; - wordIndex < msg.tb.lines[rowIndex].length; - wordIndex += 1 - ) { - const word = msg.tb.lines[rowIndex][wordIndex]; - const wordWidth = text.width(word); - - // Get the word's position from our mapping - const wordMapKey = `${rowIndex}-${wordIndex}`; - const wordPosition = wordToPositionMap.get(wordMapKey); - - if (!wordPosition) { - cursorX += wordWidth; - if (wordIndex < msg.tb.lines[rowIndex].length - 1) { - cursorX += text.width(" "); - } - continue; - } - - const { startIndex: wordStartIndex, endIndex: wordEndIndex } = wordPosition; // Find which element (if any) this word range overlaps with - const overlappingElements = []; - - for (const element of parsedElements) { - // Check if word's range has any overlap with element's range - const overlapStart = Math.max(wordStartIndex, element.start); - const overlapEnd = Math.min(wordEndIndex, element.end); - const overlapLength = Math.max(0, overlapEnd - overlapStart); - - if (overlapLength > 0) { - overlappingElements.push({ - element, - overlapStart, - overlapEnd, - overlapLength - }); - } - } + // Create a modern color-coded message using \color\ syntax like KidLisp + let colorCodedMessage = msg.fullMessage; + let msgColor = "white"; + let inBox = msg.lastLayout?.inBox || false; - // Process each overlapping element - for (const overlap of overlappingElements) { - const { element, overlapStart, overlapEnd } = overlap; - - // Calculate the portion of the word that corresponds to this element - const elementStartInWord = Math.max(0, overlapStart - wordStartIndex); - const elementEndInWord = Math.min(word.length, overlapEnd - wordStartIndex); - const elementText = word.substring(elementStartInWord, elementEndInWord); - - if (elementText.length === 0) continue; - - if (element.type === "handle") { - // Handles are stored as individual segments - handles.push({ - word: elementText, - color: "pink", - x: cursorX + text.width(word.substring(0, elementStartInWord)), - y: rowY, - width: text.width(elementText), - height: rowHeight, - }); - } else { - // For URLs and prompts, use containers - let container = activeContainers.get(element.text); - - if (!container) { - // Create new container for this element - container = { - text: element.text, - lines: [], - }; - - if (element.type === "url") { - urls.push(container); - } else if (element.type === "prompt") { - prompts.push(container); - } - - activeContainers.set(element.text, container); - } // Add this portion to the container - container.lines.push({ - text: elementText, - x: cursorX + text.width(word.substring(0, elementStartInWord)), - y: rowY, - width: text.width(elementText), - height: rowHeight, - }); - } - } - - cursorX += wordWidth; - if (wordIndex < msg.tb.lines[rowIndex].length - 1) { - cursorX += text.width(" "); - } + // Parse elements from the message + const parsedElements = parseMessageElements(msg.fullMessage); + + // Sort elements by start position (reverse order for safe replacement) + parsedElements.sort((a, b) => b.start - a.start); + + // Replace each element with color-coded version + for (const element of parsedElements) { + const elementText = msg.fullMessage.substring(element.start, element.end); + let colorCodedText = ""; + + if (element.type === "handle") { + colorCodedText = `\\pink\\${elementText}\\white\\`; + } else if (element.type === "url") { + colorCodedText = `\\cyan\\${elementText}\\white\\`; + } else if (element.type === "prompt") { + colorCodedText = `\\yellow\\${elementText}\\white\\`; } - }// Debug: Log the parsed elements and text box lines for URLs (can be removed once mapping is confirmed stable) - // if (parsedElements.some(e => e.type === "url")) { - // console.log("📝 Original message:", paintableMessage); - // console.log("📦 Text box lines:", msg.tb.lines); - // console.log("🗺️ Word position map:", Array.from(wordToPositionMap.entries())); - // } // Debug: Log segment counts for multi-line elements (can be removed once confirmed stable) - // urls.forEach((url) => { - // console.log(`🕸️ URL "${url.text}" has ${url.lines.length} segments`); - // }); - // - // prompts.forEach((prompt) => { - // console.log(`💬 Prompt "${prompt.text}" has ${prompt.lines.length} segments`); - // }); + + // Replace the original text with the color-coded version + colorCodedMessage = + colorCodedMessage.substring(0, element.start) + + colorCodedText + + colorCodedMessage.substring(element.end); + } + // Create layout using the color-coded message const timestamp = { x: text.width(msg.tb.lines[msg.tb.lines.length - 1]) + 6, y: y + (msg.tb.lines.length - 1) * rowHeight, @@ -990,6 +816,7 @@ function computeMessagesLayout({ screen, text }, chat) { width: text.width(timeAgo(msg.when)), }; let timestampColor = [100 / 1.3, 100 / 1.3, 145 / 1.3]; + msg.layout = { x: leftMargin, y, @@ -999,10 +826,10 @@ function computeMessagesLayout({ screen, text }, chat) { timestampColor, msgColor, inBox, - handles, - prompts, - urls, - paintableMessage, + handles: [], // No longer needed with color codes + prompts: [], // No longer needed with color codes + urls: [], // No longer needed with color codes + paintableMessage: colorCodedMessage, }; delete msg.lastLayout; diff --git a/system/public/aesthetic.computer/lib/disk.mjs b/system/public/aesthetic.computer/lib/disk.mjs index 66cdf0e047..dfa311f032 100644 --- a/system/public/aesthetic.computer/lib/disk.mjs +++ b/system/public/aesthetic.computer/lib/disk.mjs @@ -2277,7 +2277,31 @@ const $commonApi = { // Get the pixel width of a string of characters. width: (text) => { if (Array.isArray(text)) text = text.join(" "); - return text.length * 6; + + // Use the current typeface for accurate width calculation + const useTypeface = tf; + if (!useTypeface) { + return text.length * DEFAULT_TYPEFACE_BLOCK_WIDTH; + } + + const isProportional = + useTypeface?.data?.proportional === true || + !!useTypeface?.data?.advances || + !!useTypeface?.data?.bdfFont; + + if (isProportional && typeof useTypeface.getAdvance === "function") { + let totalWidth = 0; + for (const char of text) { + if (char === "\n" || char === "\r") continue; + const advance = useTypeface.getAdvance(char); + totalWidth += typeof advance === "number" ? advance : useTypeface.blockWidth; + } + return totalWidth; + } else { + // For monospace fonts, use the typeface's block width + const blockWidth = useTypeface.blockWidth || DEFAULT_TYPEFACE_BLOCK_WIDTH; + return text.length * blockWidth; + } }, height: (text) => { // Get the pixel height of a string of characters. -- knotmirror 0.0.1 From 15c2f0d6121181f21364fd74f2f058f17a736b29 Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Sun, 28 Sep 2025 09:33:32 +0000 Subject: [PATCH 6/7] link fix --- .../public/aesthetic.computer/disks/chat.mjs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/system/public/aesthetic.computer/disks/chat.mjs b/system/public/aesthetic.computer/disks/chat.mjs index 0fb7edd01d..bf5ec4eebb 100644 --- a/system/public/aesthetic.computer/disks/chat.mjs +++ b/system/public/aesthetic.computer/disks/chat.mjs @@ -994,3 +994,47 @@ function parseMessageElements(message) { return elements; } + +// Calculate the rendered position of an interactive element in the text layout +function calculateElementPosition(element, fullMessage, textLines, text) { + // Find which line(s) and character positions this element spans + let charCount = 0; + + for (let lineIndex = 0; lineIndex < textLines.length; lineIndex++) { + const lineText = textLines[lineIndex]; // This is already a string + const lineStart = charCount; + const lineEnd = charCount + lineText.length; + + // Check if element starts in this line + if (element.start >= lineStart && element.start <= lineEnd) { + const startInLine = element.start - lineStart; + const endInLine = Math.min(element.end - lineStart, lineText.length); + + // Calculate pixel position + const startX = text.width(lineText.substring(0, startInLine)); + const width = text.width(lineText.substring(startInLine, endInLine)); + + return { + x: startX, + y: lineIndex * rowHeight, + width: width, + height: rowHeight, + lineIndex: lineIndex + }; + } + + charCount += lineText.length + 1; // +1 for space between lines + } + + return null; // Element not found in layout +} + +// Check if a click position is inside an element's bounds +function isClickInsideElement(clickX, clickY, elementPosition) { + return ( + clickX >= elementPosition.x && + clickX <= elementPosition.x + elementPosition.width && + clickY >= elementPosition.y && + clickY <= elementPosition.y + elementPosition.height + ); +} -- knotmirror 0.0.1 From 1f958295a780759163cf4f5ac656b461072c3ec0 Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Sun, 28 Sep 2025 09:47:14 +0000 Subject: [PATCH 7/7] fix chat links --- .../public/aesthetic.computer/disks/chat.mjs | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/system/public/aesthetic.computer/disks/chat.mjs b/system/public/aesthetic.computer/disks/chat.mjs index bf5ec4eebb..80e62babb7 100644 --- a/system/public/aesthetic.computer/disks/chat.mjs +++ b/system/public/aesthetic.computer/disks/chat.mjs @@ -293,9 +293,12 @@ function paint( // 🪧 Paint the message and its contents. ink("white", layout.inBox ? 64 : 32).box(x, y, tb.box.width, tb.box.height); + // Generate dynamic color-coded message with hover states + const dynamicColorMessage = generateDynamicColorMessage(message); + // Render the color-coded message using the modern color system ink(layout.msgColor).write( - layout.paintableMessage, + dynamicColorMessage, { x, y }, undefined, screen.width - x, @@ -505,6 +508,32 @@ function act( break; } + // Check for hover on interactive elements + const parsedElements = parseMessageElements(message.fullMessage); + const relativeX = e.x - message.layout.x; + const relativeY = e.y - message.layout.y; + + // Reset hover states + if (!message.layout.hoveredElements) { + message.layout.hoveredElements = new Set(); + } + message.layout.hoveredElements.clear(); + + // Check each interactive element for hover + for (const element of parsedElements) { + const elementPosition = calculateElementPosition( + element, + message.fullMessage, + message.tb.lines, + text + ); + + if (elementPosition && isClickInsideElement(relativeX, relativeY, elementPosition)) { + message.layout.hoveredElements.add(element); + break; // Only hover one element at a time + } + } + // Store the clicked message for potential interaction message.clicked = true; @@ -573,6 +602,11 @@ function act( if (timestamp.over) { timestamp.over = false; } + + // Clear hover states on lift + if (message.layout.hoveredElements) { + message.layout.hoveredElements.clear(); + } } } @@ -1038,3 +1072,45 @@ function isClickInsideElement(clickX, clickY, elementPosition) { clickY <= elementPosition.y + elementPosition.height ); } + +// Generate a color-coded message with dynamic hover states +function generateDynamicColorMessage(message) { + let colorCodedMessage = message.fullMessage; + const parsedElements = parseMessageElements(message.fullMessage); + const hoveredElements = message.layout.hoveredElements || new Set(); + + // Sort elements by start position (reverse order for safe replacement) + parsedElements.sort((a, b) => b.start - a.start); + + // Replace each element with color-coded version + for (const element of parsedElements) { + const elementText = message.fullMessage.substring(element.start, element.end); + let colorCodedText = ""; + + // Check if this element is being hovered + const isHovered = Array.from(hoveredElements).some(hoveredEl => + hoveredEl.start === element.start && + hoveredEl.end === element.end && + hoveredEl.type === element.type + ); + + if (element.type === "handle") { + const color = isHovered ? "yellow" : "pink"; + colorCodedText = `\\${color}\\${elementText}\\white\\`; + } else if (element.type === "url") { + const color = isHovered ? "yellow" : "cyan"; + colorCodedText = `\\${color}\\${elementText}\\white\\`; + } else if (element.type === "prompt") { + const color = isHovered ? "yellow" : "lime"; + colorCodedText = `\\${color}\\${elementText}\\white\\`; + } + + // Replace the original text with the color-coded version + colorCodedMessage = + colorCodedMessage.substring(0, element.start) + + colorCodedText + + colorCodedMessage.substring(element.end); + } + + return colorCodedMessage; +}