| 1284 | * Handles wide character boundary cleanup at region edges. |
| 1285 | */ |
| 1286 | export function clearRegion( |
| 1287 | screen: Screen, |
| 1288 | regionX: number, |
| 1289 | regionY: number, |
| 1290 | regionWidth: number, |
| 1291 | regionHeight: number, |
| 1292 | ): void { |
| 1293 | const startX = Math.max(0, regionX) |
| 1294 | const startY = Math.max(0, regionY) |
| 1295 | const maxX = Math.min(regionX + regionWidth, screen.width) |
| 1296 | const maxY = Math.min(regionY + regionHeight, screen.height) |
| 1297 | if (startX >= maxX || startY >= maxY) return |
| 1298 | |
| 1299 | const cells = screen.cells |
| 1300 | const cells64 = screen.cells64 |
| 1301 | const screenWidth = screen.width |
| 1302 | const rowBase = startY * screenWidth |
| 1303 | let damageMinX = startX |
| 1304 | let damageMaxX = maxX |
| 1305 | |
| 1306 | // EMPTY_CELL_VALUE (0n) matches the zero-initialized state: |
| 1307 | // word0=EMPTY_CHAR_INDEX(0), word1=packWord1(0,0,0)=0 |
| 1308 | if (startX === 0 && maxX === screenWidth) { |
| 1309 | // Full-width: single fill, no boundary checks needed |
| 1310 | cells64.fill( |
| 1311 | EMPTY_CELL_VALUE, |
| 1312 | rowBase, |
| 1313 | rowBase + (maxY - startY) * screenWidth, |
| 1314 | ) |
| 1315 | } else { |
| 1316 | // Partial-width: single loop handles boundary cleanup and fill per row. |
| 1317 | const stride = screenWidth << 1 // 2 Int32s per cell |
| 1318 | const rowLen = maxX - startX |
| 1319 | const checkLeft = startX > 0 |
| 1320 | const checkRight = maxX < screenWidth |
| 1321 | let leftEdge = (rowBase + startX) << 1 |
| 1322 | let rightEdge = (rowBase + maxX - 1) << 1 |
| 1323 | let fillStart = rowBase + startX |
| 1324 | |
| 1325 | for (let y = startY; y < maxY; y++) { |
| 1326 | // Left boundary: if cell at startX is a SpacerTail, the Wide char |
| 1327 | // at startX-1 (outside the region) will be orphaned. Clear it. |
| 1328 | if (checkLeft) { |
| 1329 | // leftEdge points to word0 of cell at startX; +1 is its word1 |
| 1330 | if ((cells[leftEdge + 1]! & WIDTH_MASK) === CellWidth.SpacerTail) { |
| 1331 | // word1 of cell at startX-1 is leftEdge-1; word0 is leftEdge-2 |
| 1332 | const prevW1 = leftEdge - 1 |
| 1333 | if ((cells[prevW1]! & WIDTH_MASK) === CellWidth.Wide) { |
| 1334 | cells[prevW1 - 1] = EMPTY_CHAR_INDEX |
| 1335 | cells[prevW1] = packWord1(screen.emptyStyleId, 0, CellWidth.Narrow) |
| 1336 | damageMinX = startX - 1 |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | // Right boundary: if cell at maxX-1 is Wide, its SpacerTail at maxX |
| 1342 | // (outside the region) will be orphaned. Clear it. |
| 1343 | if (checkRight) { |