-
Notifications
You must be signed in to change notification settings - Fork 3
/
ANSIPrint.bas
791 lines (599 loc) · 36.8 KB
/
ANSIPrint.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
'-----------------------------------------------------------------------------------------------------------------------
' ANSI Escape Sequence Emulator
' Copyright (c) 2024 Samuel Gomes
'
' TODO:
' https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#screen-colors
' https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#window-title
' https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#soft-reset
' https://github.com/a740g/ANSIPrint/blob/master/docs/ansimtech.txt
' https://conemu.github.io/en/AnsiEscapeCodes.html
'-----------------------------------------------------------------------------------------------------------------------
$INCLUDEONCE
'$INCLUDE:'ANSIPrint.bi'
'-------------------------------------------------------------------------------------------------------------------
' TEST CODE
'-------------------------------------------------------------------------------------------------------------------
'$DEBUG
'SCREEN _NEWIMAGE(8 * 80, 16 * 28, 32)
'SCREEN _NEWIMAGE(8 * 80, 16 * 28, 9)
'SCREEN _NEWIMAGE(8 * 80, 16 * 28, 12)
'SCREEN _NEWIMAGE(8 * 80, 16 * 28, 13)
'SCREEN _NEWIMAGE(80, 28, 0)
'_FONT 16
'DO
' DIM ansFile AS STRING: ansFile = _OPENFILEDIALOG$("Open", "", "*.ans|*.asc|*.diz|*.nfo|*.txt", "ANSI Art Files")
' IF NOT _FILEEXISTS(ansFile) THEN EXIT DO
' DIM fh AS LONG: fh = FREEFILE
' OPEN ansFile FOR BINARY ACCESS READ AS fh
' COLOR _RGB(170, 170, 170), _RGB(0, 0, 0)
' CLS
' ANSI_Print INPUT$(LOF(fh), fh)
' CLOSE fh
' _TITLE "Press any key to open another file...": SLEEP 3600
'LOOP
'END
'-------------------------------------------------------------------------------------------------------------------
' Initializes library global variables and tables and then sets the init flag to true
SUB ANSI_InitializeEmulator
SHARED __ANSIEmu AS __ANSIEmulatorType
SHARED __ANSIColorLUT() AS BGRType
SHARED __ANSIArg() AS LONG
IF __ANSIEmu.isInitialized THEN EXIT SUB ' leave if we have already initialized
' The first 16 are the standard 16 ANSI colors (matches QB64's VGA palette but with different color indices!)
__ANSIColorLUT(0).r = 0: __ANSIColorLUT(0).g = 0: __ANSIColorLUT(0).b = 0 ' 0: _RGB32(0, 0, 0) (black)
__ANSIColorLUT(1).r = 170: __ANSIColorLUT(1).g = 0: __ANSIColorLUT(1).b = 0 ' 1: _RGB32(170, 0, 0) (red)
__ANSIColorLUT(2).r = 0: __ANSIColorLUT(2).g = 170: __ANSIColorLUT(2).b = 0 ' 2: _RGB32(0, 170, 0) (green)
__ANSIColorLUT(3).r = 170: __ANSIColorLUT(3).g = 85: __ANSIColorLUT(3).b = 0 ' 3: _RGB32(170, 85, 0) (brown)
__ANSIColorLUT(4).r = 0: __ANSIColorLUT(4).g = 0: __ANSIColorLUT(4).b = 170 ' 4: _RGB32(0, 0, 170) (blue)
__ANSIColorLUT(5).r = 170: __ANSIColorLUT(5).g = 0: __ANSIColorLUT(5).b = 170 ' 5: _RGB32(170, 0, 170) (magenta)
__ANSIColorLUT(6).r = 0: __ANSIColorLUT(6).g = 170: __ANSIColorLUT(6).b = 170 ' 6: _RGB32(0, 170, 170) (cyan)
__ANSIColorLUT(7).r = 170: __ANSIColorLUT(7).g = 170: __ANSIColorLUT(7).b = 170 ' 7: _RGB32(170, 170, 170) (white)
__ANSIColorLUT(8).r = 85: __ANSIColorLUT(8).g = 85: __ANSIColorLUT(8).b = 85 ' 8: _RGB32(85, 85, 85) (grey)
__ANSIColorLUT(9).r = 255: __ANSIColorLUT(9).g = 85: __ANSIColorLUT(9).b = 85 ' 9: _RGB32(255, 85, 85) (bright red)
__ANSIColorLUT(10).r = 85: __ANSIColorLUT(10).g = 255: __ANSIColorLUT(10).b = 85 ' 10: _RGB32(85, 255, 85) (bright green)
__ANSIColorLUT(11).r = 255: __ANSIColorLUT(11).g = 255: __ANSIColorLUT(11).b = 85 ' 11: _RGB32(255, 255, 85) (bright yellow)
__ANSIColorLUT(12).r = 85: __ANSIColorLUT(12).g = 85: __ANSIColorLUT(12).b = 255 ' 12: _RGB32(85, 85, 255) (bright blue)
__ANSIColorLUT(13).r = 255: __ANSIColorLUT(13).g = 85: __ANSIColorLUT(13).b = 255 ' 13: _RGB32(255, 85, 255) (bright magenta)
__ANSIColorLUT(14).r = 85: __ANSIColorLUT(14).g = 255: __ANSIColorLUT(14).b = 255 ' 14: _RGB32(85, 255, 255) (bright cyan)
__ANSIColorLUT(15).r = 255: __ANSIColorLUT(15).g = 255: __ANSIColorLUT(15).b = 255 ' 15: _RGB32(255, 255, 255) (bright white)
' The next 216 colors (16 - 231) are formed by a 3bpc RGB value offset by 16
DIM AS LONG c, i
FOR c = 16 TO 231
i = ((c - 16) \ 36) MOD 6
IF i = 0 THEN __ANSIColorLUT(c).r = 0 ELSE __ANSIColorLUT(c).r = (14135 + 10280 * i) \ 256
i = ((c - 16) \ 6) MOD 6
IF i = 0 THEN __ANSIColorLUT(c).g = 0 ELSE __ANSIColorLUT(c).g = (14135 + 10280 * i) \ 256
i = ((c - 16) \ 1) MOD 6
IF i = 0 THEN __ANSIColorLUT(c).b = 0 ELSE __ANSIColorLUT(c).b = (14135 + 10280 * i) \ 256
NEXT
' The final 24 colors (232 - 255) are grayscale starting from a shade slighly lighter than black, ranging up to shade slightly darker than white
FOR c = 232 TO 255
i = (2056 + 2570 * (c - 232)) \ 256
__ANSIColorLUT(c).r = i
__ANSIColorLUT(c).g = i
__ANSIColorLUT(c).b = i
NEXT
REDIM __ANSIArg(1 TO UBOUND(__ANSIArg)) AS LONG ' reset the CSI arg list
__ANSIEmu.state = __ANSI_STATE_TEXT ' we will start parsing regular text by default
__ANSIEmu.argIndex = 0 ' reset argument index
' Reset the foreground and background color
__ANSIEmu.fC = __ANSI_DEFAULT_COLOR_FOREGROUND
ANSI_SetTextCanvasColor __ANSIEmu.fC, FALSE, TRUE
__ANSIEmu.bC = __ANSI_DEFAULT_COLOR_BACKGROUND
ANSI_SetTextCanvasColor __ANSIEmu.bC, TRUE, TRUE
' Reset text attributes
__ANSIEmu.isBold = FALSE
__ANSIEmu.isBlink = FALSE
__ANSIEmu.isInvert = FALSE
' Get the current cursor position
__ANSIEmu.posDEC.x = POS(0)
__ANSIEmu.posDEC.y = CSRLIN
__ANSIEmu.posSCO = __ANSIEmu.posDEC
__ANSIEmu.lastChar = NULL
__ANSIEmu.lastCharX = NULL
__ANSIEmu.CPS = 0 ' disable any speed control
IF _PIXELSIZE = 0 THEN
_BLINK OFF ' use ICE colors
ELSE
_PRINTMODE _FILLBACKGROUND ' set _PRINTMODE to fill the background for graphics mode
END IF
_CONTROLCHR ON ' get assist from QB64's control character handling (only for tabs; we are pretty much doing the rest ourselves)
__ANSIEmu.isInitialized = TRUE ' set to true to indicate init is done
END SUB
' This simply resets the emulator to a clean state
SUB ANSI_ResetEmulator
SHARED __ANSIEmu AS __ANSIEmulatorType
__ANSIEmu.isInitialized = FALSE ' set the init flag to false
ANSI_InitializeEmulator ' call the init routine
END SUB
' Sets the emulation speed
' nCPS - characters / second (bigger numbers means faster; <= 0 to disable)
SUB ANSI_SetEmulationSpeed (nCPS AS LONG)
SHARED __ANSIEmu AS __ANSIEmulatorType
__ANSIEmu.CPS = nCPS
END SUB
' Processes a single byte and decides what to do with it based on the current emulation state
FUNCTION ANSI_PrintCharacter%% (ch AS _UNSIGNED _BYTE)
SHARED __ANSIEmu AS __ANSIEmulatorType
SHARED __ANSIArg() AS LONG
ANSI_PrintCharacter = TRUE ' by default we will return true to tell the caller to keep going
DIM AS LONG x, y, z ' temp variables used in many places (usually as counter / index)
SELECT CASE __ANSIEmu.state
CASE __ANSI_STATE_TEXT ' handle normal characters (including some control characters)
SELECT CASE ch
CASE ANSI_SUB ' stop processing and exit loop on EOF (usually put by SAUCE blocks)
__ANSIEmu.state = __ANSI_STATE_END
CASE ANSI_BEL ' handle Bell - because QB64 does not (even with ControlChr On)
BEEP
CASE ANSI_BS ' handle Backspace - because QB64 does not (even with ControlChr On)
x = POS(0) - 1
IF x > 0 THEN LOCATE , x ' move to the left only if we are not on the edge
CASE ANSI_LF ' handle Line Feed (including EOL CRLF special case)
PRINT STRING$(1 - (ANSI_CR = __ANSIEmu.lastChar AND ANSI_GetTextCanvasWidth = __ANSIEmu.lastCharX), ch);
CASE ANSI_FF ' handle Form Feed - because QB64 does not (even with ControlChr On)
LOCATE 1, 1
CASE ANSI_CR ' handle Carriage Return because QB64 screws this up and moves the cursor to the beginning of the next line
LOCATE , 1
'Case ANSI_DEL ' TODO: Check what to do with this
CASE ANSI_ESC ' handle escape character
__ANSIEmu.state = __ANSI_STATE_BEGIN ' beginning a new escape sequence
CASE ANSI_RS, ANSI_US ' QB64 does non-ANSI stuff with these two when ControlChar is On
_CONTROLCHR OFF
__ANSIEmu.lastCharX = POS(0)
PRINT CHR$(ch); ' print escaped ESC character
_CONTROLCHR ON
IF __ANSIEmu.CPS > 0 THEN _LIMIT __ANSIEmu.CPS ' limit the loop speed if char/sec is a positive value
CASE ELSE ' print the character
__ANSIEmu.lastCharX = POS(0)
PRINT CHR$(ch);
IF __ANSIEmu.CPS > 0 THEN _LIMIT __ANSIEmu.CPS ' limit the loop speed if char/sec is a positive value
END SELECT
CASE __ANSI_STATE_BEGIN ' handle escape sequence
SELECT CASE ch
CASE IS < ANSI_SP ' handle escaped character
_CONTROLCHR OFF
__ANSIEmu.lastCharX = POS(0)
PRINT CHR$(ch); ' print escaped ESC character
_CONTROLCHR ON
IF __ANSIEmu.CPS > 0 THEN _LIMIT __ANSIEmu.CPS ' limit the loop speed if char/sec is a positive value
__ANSIEmu.state = __ANSI_STATE_TEXT
CASE ANSI_ESC_DECSC ' Save Cursor Position in Memory
__ANSIEmu.posDEC.x = POS(0)
__ANSIEmu.posDEC.y = CSRLIN
__ANSIEmu.state = __ANSI_STATE_TEXT
CASE ANSI_ESC_DECSR ' Restore Cursor Position from Memory
LOCATE __ANSIEmu.posDEC.y, __ANSIEmu.posDEC.x
__ANSIEmu.state = __ANSI_STATE_TEXT
CASE ANSI_ESC_RI ' Reverse Index
y = CSRLIN - 1
IF y > 0 THEN LOCATE y
__ANSIEmu.state = __ANSI_STATE_TEXT
CASE ANSI_ESC_CSI ' handle CSI
REDIM __ANSIArg(1 TO UBOUND(__ANSIArg)) AS LONG ' reset the control sequence arguments, but don't loose the allocated memory
__ANSIEmu.argIndex = 0 ' reset argument index
'leadInPrefix = 0 ' reset lead-in prefix
__ANSIEmu.state = __ANSI_STATE_SEQUENCE
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE __ANSI_STATE_SEQUENCE ' handle CSI sequence
SELECT CASE ch
CASE ANSI_0 TO ANSI_QUESTION_MARK ' argument bytes
IF __ANSIEmu.argIndex < 1 THEN __ANSIEmu.argIndex = 1 ' set the argument index to one if this is the first time
SELECT CASE ch
CASE ANSI_0 TO ANSI_9 ' handle sequence numeric arguments
__ANSIArg(__ANSIEmu.argIndex) = __ANSIArg(__ANSIEmu.argIndex) * 10 + ch - ANSI_0
CASE ANSI_SEMICOLON ' handle sequence argument seperators
__ANSIEmu.argIndex = __ANSIEmu.argIndex + 1 ' increment the argument index
IF __ANSIEmu.argIndex > UBOUND(__ANSIArg) THEN REDIM _PRESERVE __ANSIArg(1 TO __ANSIEmu.argIndex) AS LONG ' dynamically expand the argument list if needed
CASE ANSI_EQUALS_SIGN, ANSI_GREATER_THAN_SIGN, ANSI_QUESTION_MARK ' handle lead-in prefix
' NOP: leadInPrefix = ch ' just save the prefix type
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE ANSI_SP TO ANSI_SLASH ' intermediate bytes
SELECT CASE ch
CASE ANSI_SP ' ignore spaces
' NOP
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE ANSI_AT_SIGN TO ANSI_TILDE ' final byte
SELECT CASE ch
CASE ANSI_ESC_CSI_SM, ANSI_ESC_CSI_RM ' Set and reset screen mode
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
SELECT CASE __ANSIArg(1)
CASE 0 TO 6, 14 TO 18 ' all mode changes are ignored. the screen type must be set by the caller
' NOP
CASE 7 ' Enable / disable line wrapping
' NOP: QB64 does line wrapping by default
IF ANSI_ESC_CSI_RM = ch THEN ' ANSI_ESC_CSI_RM disable line wrapping unsupported
ERROR ERROR_FEATURE_UNAVAILABLE
END IF
CASE 12 ' Text Cursor Enable / Disable Blinking
' NOP
CASE 25 ' make cursor visible / invisible
IF ANSI_ESC_CSI_SM = ch THEN ' ANSI_ESC_CSI_SM make cursor visible
LOCATE , , 1
ELSE ' ANSI_ESC_CSI_RM make cursor invisible
LOCATE , , 0
END IF
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE ANSI_ESC_CSI_ED ' Erase in Display
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
SELECT CASE __ANSIArg(1)
CASE 0 ' clear from cursor to end of screen
ANSI_ClearTextCanvasArea POS(0), CSRLIN, ANSI_GetTextCanvasWidth, CSRLIN ' first clear till the end of the line starting from the cursor
ANSI_ClearTextCanvasArea 1, CSRLIN + 1, ANSI_GetTextCanvasWidth, ANSI_GetTextCanvasHeight ' next clear the whole canvas below the cursor
CASE 1 ' clear from cursor to beginning of the screen
ANSI_ClearTextCanvasArea 1, CSRLIN, POS(0), CSRLIN ' first clear from the beginning of the line till the cursor
ANSI_ClearTextCanvasArea 1, 1, ANSI_GetTextCanvasWidth, CSRLIN - 1 ' next clear the whole canvas above the cursor
CASE 2 ' clear entire screen (and moves cursor to upper left like ANSI.SYS)
CLS
CASE 3 ' clear entire screen and delete all lines saved in the scrollback buffer (scrollback stuff not supported)
ANSI_ClearTextCanvasArea 1, 1, ANSI_GetTextCanvasWidth, ANSI_GetTextCanvasHeight
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE ANSI_ESC_CSI_EL ' Erase in Line
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
SELECT CASE __ANSIArg(1)
CASE 0 ' erase from cursor to end of line
ANSI_ClearTextCanvasArea POS(0), CSRLIN, ANSI_GetTextCanvasWidth, CSRLIN
CASE 1 ' erase start of line to the cursor
ANSI_ClearTextCanvasArea 1, CSRLIN, POS(0), CSRLIN
CASE 2 ' erase the entire line
ANSI_ClearTextCanvasArea 1, CSRLIN, ANSI_GetTextCanvasWidth, CSRLIN
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE ANSI_ESC_CSI_SGR ' Select Graphic Rendition
x = 1 ' start with the first argument
IF __ANSIEmu.argIndex < 1 THEN __ANSIEmu.argIndex = 1 ' this allows '[m' to be treated as [0m
DO WHILE x <= __ANSIEmu.argIndex ' loop through the argument list and process each argument
SELECT CASE __ANSIArg(x)
CASE 0 ' reset all modes (styles and colors)
__ANSIEmu.fC = __ANSI_DEFAULT_COLOR_FOREGROUND
__ANSIEmu.bC = __ANSI_DEFAULT_COLOR_BACKGROUND
__ANSIEmu.isBold = FALSE
__ANSIEmu.isBlink = FALSE
__ANSIEmu.isInvert = FALSE
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
CASE 1 ' enable high intensity colors
IF __ANSIEmu.fC < 8 THEN __ANSIEmu.fC = __ANSIEmu.fC + 8
__ANSIEmu.isBold = TRUE
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
CASE 2, 22 ' enable low intensity, disable high intensity colors
IF __ANSIEmu.fC > 7 THEN __ANSIEmu.fC = __ANSIEmu.fC - 8
__ANSIEmu.isBold = FALSE
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
CASE 3, 4, 23, 24 ' set / reset italic & underline mode ignored
' NOP: This can be used if we load monospaced TTF fonts using 'italics', 'underline' properties
CASE 5, 6 ' turn blinking on
IF __ANSIEmu.bC < 8 THEN __ANSIEmu.bC = __ANSIEmu.bC + 8
__ANSIEmu.isBlink = TRUE
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
CASE 7 ' enable reverse video
IF NOT __ANSIEmu.isInvert THEN
__ANSIEmu.isInvert = TRUE
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
END IF
CASE 25 ' turn blinking off
IF __ANSIEmu.bC > 7 THEN __ANSIEmu.bC = __ANSIEmu.bC - 8
__ANSIEmu.isBlink = FALSE
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
CASE 27 ' disable reverse video
IF __ANSIEmu.isInvert THEN
__ANSIEmu.isInvert = FALSE
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
END IF
CASE 30 TO 37 ' set foreground color
__ANSIEmu.fC = __ANSIArg(x) - 30
IF __ANSIEmu.isBold THEN __ANSIEmu.fC = __ANSIEmu.fC + 8
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
CASE 38 ' set 8-bit 256 or 24-bit RGB foreground color
z = __ANSIEmu.argIndex - x ' get the number of arguments remaining
IF __ANSIArg(x + 1) = 2 AND z >= 4 THEN ' 32bpp color with 5 arguments
__ANSIEmu.fC = _RGB32(__ANSIArg(x + 2) AND &HFF, __ANSIArg(x + 3) AND &HFF, __ANSIArg(x + 4) AND &HFF)
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, FALSE
x = x + 4 ' skip to last used arg
ELSEIF __ANSIArg(x + 1) = 5 AND z >= 2 THEN ' 256 color with 3 arguments
__ANSIEmu.fC = __ANSIArg(x + 2)
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
x = x + 2 ' skip to last used arg
ELSE
ERROR ERROR_CANNOT_CONTINUE
END IF
CASE 39 ' set default foreground color
__ANSIEmu.fC = __ANSI_DEFAULT_COLOR_FOREGROUND
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
CASE 40 TO 47 ' set background color
__ANSIEmu.bC = __ANSIArg(x) - 40
IF __ANSIEmu.isBlink THEN __ANSIEmu.bC = __ANSIEmu.bC + 8
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
CASE 48 ' set 8-bit 256 or 24-bit RGB background color
z = __ANSIEmu.argIndex - x ' get the number of arguments remaining
IF __ANSIArg(x + 1) = 2 AND z >= 4 THEN ' 32bpp color with 5 arguments
__ANSIEmu.bC = _RGB32(__ANSIArg(x + 2) AND &HFF, __ANSIArg(x + 3) AND &HFF, __ANSIArg(x + 4) AND &HFF)
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, FALSE
x = x + 4 ' skip to last used arg
ELSEIF __ANSIArg(x + 1) = 5 AND z >= 2 THEN ' 256 color with 3 arguments
__ANSIEmu.bC = __ANSIArg(x + 2)
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
x = x + 2 ' skip to last used arg
ELSE
ERROR ERROR_CANNOT_CONTINUE
END IF
CASE 49 ' set default background color
__ANSIEmu.bC = __ANSI_DEFAULT_COLOR_BACKGROUND
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
CASE 90 TO 97 ' set high intensity foreground color
__ANSIEmu.fC = 8 + __ANSIArg(x) - 90
ANSI_SetTextCanvasColor __ANSIEmu.fC, __ANSIEmu.isInvert, TRUE
CASE 100 TO 107 ' set high intensity background color
__ANSIEmu.bC = 8 + __ANSIArg(x) - 100
ANSI_SetTextCanvasColor __ANSIEmu.bC, NOT __ANSIEmu.isInvert, TRUE
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
x = x + 1 ' move to the next argument
LOOP
CASE ANSI_ESC_CSI_SCP ' Save Current Cursor Position (SCO)
IF __ANSIEmu.argIndex > 0 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting args
__ANSIEmu.posSCO.x = POS(0)
__ANSIEmu.posSCO.y = CSRLIN
CASE ANSI_ESC_CSI_RCP ' Restore Saved Cursor Position (SCO)
IF __ANSIEmu.argIndex > 0 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting args
LOCATE __ANSIEmu.posSCO.y, __ANSIEmu.posSCO.x
CASE ANSI_ESC_CSI_PABLODRAW_24BPP ' PabloDraw 24-bit ANSI sequences
IF __ANSIEmu.argIndex <> 4 THEN ERROR ERROR_CANNOT_CONTINUE ' we need 4 arguments
ANSI_SetTextCanvasColor _RGB32(__ANSIArg(2) AND &HFF, __ANSIArg(3) AND &HFF, __ANSIArg(4) AND &HFF), __ANSIArg(1) = FALSE, FALSE
CASE ANSI_ESC_CSI_CUP, ANSI_ESC_CSI_HVP ' Cursor position or Horizontal and vertical position
IF __ANSIEmu.argIndex > 2 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 2 args
y = ANSI_GetTextCanvasHeight
IF __ANSIArg(1) < 1 THEN
__ANSIArg(1) = 1
ELSEIF __ANSIArg(1) > y THEN
__ANSIArg(1) = y
END IF
x = ANSI_GetTextCanvasWidth
IF __ANSIArg(2) < 1 THEN
__ANSIArg(2) = 1
ELSEIF __ANSIArg(2) > x THEN
__ANSIArg(2) = x
END IF
LOCATE __ANSIArg(1), __ANSIArg(2) ' line #, column #
CASE ANSI_ESC_CSI_CUU ' Cursor up
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
IF __ANSIArg(1) < 1 THEN __ANSIArg(1) = 1
y = CSRLIN - __ANSIArg(1)
IF y < 1 THEN y = 1
LOCATE y
CASE ANSI_ESC_CSI_CUD ' Cursor down
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
IF __ANSIArg(1) < 1 THEN __ANSIArg(1) = 1
y = CSRLIN + __ANSIArg(1)
z = ANSI_GetTextCanvasHeight
IF y > z THEN y = z
LOCATE y
CASE ANSI_ESC_CSI_CUF ' Cursor forward
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
IF __ANSIArg(1) < 1 THEN __ANSIArg(1) = 1
x = POS(0) + __ANSIArg(1)
z = ANSI_GetTextCanvasWidth
IF x > z THEN x = z
LOCATE , x
CASE ANSI_ESC_CSI_CUB ' Cursor back
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
IF __ANSIArg(1) < 1 THEN __ANSIArg(1) = 1
x = POS(0) - __ANSIArg(1)
IF x < 1 THEN x = 1
LOCATE , x
CASE ANSI_ESC_CSI_CNL ' Cursor Next Line
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
IF __ANSIArg(1) < 1 THEN __ANSIArg(1) = 1
y = CSRLIN + __ANSIArg(1)
z = ANSI_GetTextCanvasHeight
IF y > z THEN y = z
LOCATE y, 1
CASE ANSI_ESC_CSI_CPL ' Cursor Previous Line
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
IF __ANSIArg(1) < 1 THEN __ANSIArg(1) = 1
y = CSRLIN - __ANSIArg(1)
IF y < 1 THEN y = 1
LOCATE y, 1
CASE ANSI_ESC_CSI_CHA ' Cursor Horizontal Absolute
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
x = ANSI_GetTextCanvasWidth
IF __ANSIArg(1) < 1 THEN
__ANSIArg(1) = 1
ELSEIF __ANSIArg(1) > x THEN
__ANSIArg(1) = x
END IF
LOCATE , __ANSIArg(1)
CASE ANSI_ESC_CSI_VPA ' Vertical Line Position Absolute
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
y = ANSI_GetTextCanvasHeight
IF __ANSIArg(1) < 1 THEN
__ANSIArg(1) = 1
ELSEIF __ANSIArg(1) > y THEN
__ANSIArg(1) = y
END IF
LOCATE __ANSIArg(1)
CASE ANSI_ESC_CSI_DECSCUSR
IF __ANSIEmu.argIndex > 1 THEN ERROR ERROR_CANNOT_CONTINUE ' was not expecting more than 1 arg
SELECT CASE __ANSIArg(1)
CASE 0, 3, 4 ' Default, Blinking & Steady underline cursor shape
LOCATE , , , 29, 31 ' this should give a nice underline cursor
CASE 1, 2 ' Blinking & Steady block cursor shape
LOCATE , , , 0, 31 ' this should give a full block cursor
CASE 5, 6 ' Blinking & Steady bar cursor shape
LOCATE , , , 16, 31 ' since we cannot get a bar cursor in QB64, we'll just use a half-block cursor
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
' End of sequence
__ANSIEmu.state = __ANSI_STATE_TEXT
CASE ELSE ' throw an error for stuff we are not handling
ERROR ERROR_FEATURE_UNAVAILABLE
END SELECT
CASE __ANSI_STATE_END ' end of the stream has been reached
ANSI_PrintCharacter = FALSE ' tell the caller the we should stop processing the rest of the stream
EXIT FUNCTION ' and then leave
CASE ELSE ' this should never happen
ERROR ERROR_CANNOT_CONTINUE
END SELECT
__ANSIEmu.lastChar = ch ' save the character
END FUNCTION
' Processes the whole string instead of a character like PrintANSICharacter()
' This simply wraps PrintANSICharacter()
' It returns True when EOF is encountered
FUNCTION ANSI_PrintString%% (s AS STRING)
ANSI_PrintString = TRUE
DIM AS LONG i: FOR i = 1 TO LEN(s)
IF NOT ANSI_PrintCharacter(ASC(s, i)) THEN
ANSI_PrintString = FALSE ' signal end of stream
EXIT FUNCTION
END IF
NEXT i
END FUNCTION
' A simple routine that wraps pretty much the whole library
' It will reset the library, do the setup and then render the whole ANSI string in one go
' _PRINTMODE and _CONTROLCHR is properly restored
SUB ANSI_Print (sANSI AS STRING)
' Save _PRINTMODE (if not in SCREEN 0)
IF _PIXELSIZE <> 0 THEN
DIM pm AS LONG: pm = _PRINTMODE
END IF
' Save _CONTROLCHR
DIM cc AS LONG: cc = _CONTROLCHR
ANSI_ResetEmulator ' reset the emulator
DIM dummy AS _BYTE: dummy = ANSI_PrintString(sANSI) ' print the ANSI string and ignore the return value
' Set _CONTROLCHR the way we found it
' Ewww :(
IF cc THEN
_CONTROLCHR OFF
ELSE
_CONTROLCHR ON
END IF
' Set _PRINTMODE the way we found it (if not in SCREEN 0)
IF _PIXELSIZE <> 0 THEN
' Ewww :(
SELECT CASE pm
CASE 1
_PRINTMODE _KEEPBACKGROUND
CASE 2
_PRINTMODE _ONLYBACKGROUND
CASE ELSE
_PRINTMODE _FILLBACKGROUND
END SELECT
END IF
END SUB
' Set the foreground or background color
SUB ANSI_SetTextCanvasColor (c AS _UNSIGNED LONG, isBackground AS LONG, isLegacy AS LONG)
SHARED __ANSIColorLUT() AS BGRType
DIM nRGB AS _UNSIGNED LONG
' Use _RGB to get the closest matching color index for palatted mode
' For 32-bit surfaces, it will simply return the 32-bit BGRA color passed
' This way we can support all types of modes (i.e. SCREEN 0 - 13 and 32-bit)
' Note that this will obviously hurt performance. But then, does it really matter?
IF isLegacy THEN
nRGB = _RGB(__ANSIColorLUT(c).r, __ANSIColorLUT(c).g, __ANSIColorLUT(c).b)
ELSE
nRGB = _RGB(_RED32(c), _GREEN32(c), _BLUE32(c))
END IF
' Graphics_SetBackgroundColor & Graphics_SetForegroundColor has the logic to deal with SCREEN 0 color nonsense
IF isBackground THEN
Graphics_SetBackgroundColor nRGB
ELSE
Graphics_SetForegroundColor nRGB
END IF
END SUB
' Returns the number of characters per line
FUNCTION ANSI_GetTextCanvasWidth&
IF _PIXELSIZE = 0 THEN
ANSI_GetTextCanvasWidth = _WIDTH
ELSE
DIM fw AS LONG: fw = _FONTWIDTH
IF fw = 0 THEN fw = _PRINTWIDTH("W") ' :(
ANSI_GetTextCanvasWidth = _WIDTH \ fw
END IF
END FUNCTION
' Returns the number of lines
FUNCTION ANSI_GetTextCanvasHeight&
IF _PIXELSIZE = 0 THEN
ANSI_GetTextCanvasHeight = _HEIGHT
ELSE
ANSI_GetTextCanvasHeight = _HEIGHT \ _FONTHEIGHT
END IF
END FUNCTION
' Clears a given portion of screen without disturbing the cursor location and colors
SUB ANSI_ClearTextCanvasArea (l AS LONG, t AS LONG, r AS LONG, b AS LONG)
DIM w AS LONG: w = 1 + r - l ' calculate width
IF w > 0 AND t <= b THEN ' only proceed is width is > 0 and height is > 0
' Save some stuff
DIM fc AS _UNSIGNED LONG: fc = _DEFAULTCOLOR
DIM bc AS _UNSIGNED LONG: bc = _BACKGROUNDCOLOR
DIM x AS LONG: x = POS(0)
DIM y AS LONG: y = CSRLIN
COLOR _RGB(0, 0, 0), _RGB(0, 0, 0) ' lights out
DIM blankLine AS STRING: blankLine = SPACE$(w) ' do this only once
DIM i AS LONG: FOR i = t TO b
LOCATE i, l: PRINT blankLine; ' fill with SPACE
NEXT i
' Restore saved stuff
COLOR fc, bc
LOCATE y, x
END IF
END SUB
FUNCTION ANSI_GetFontHeight~%% (sauce AS SAUCEType)
CONST __ANSI_F8_1 = "IBM VGA50"
CONST __ANSI_F8_2 = "IBM EGA43"
CONST __ANSI_F14_1 = "IBM EGA"
CONST __ANSI_F8_3 = "AMIGA"
CONST __ANSI_F8_5 = "ATARI"
CONST __ANSI_F8_4 = "C64"
DIM sauceTypeInfoString AS STRING: sauceTypeInfoString = SAUCE_GetTypeInfoString(sauce)
IF UCASE$(LEFT$(sauceTypeInfoString, LEN(__ANSI_F8_1))) = __ANSI_F8_1 THEN
ANSI_GetFontHeight = 8
ELSEIF UCASE$(LEFT$(sauceTypeInfoString, LEN(__ANSI_F8_2))) = __ANSI_F8_2 THEN
ANSI_GetFontHeight = 8
ELSEIF UCASE$(LEFT$(sauceTypeInfoString, LEN(__ANSI_F14_1))) = __ANSI_F14_1 THEN
ANSI_GetFontHeight = 14
ELSEIF UCASE$(LEFT$(sauceTypeInfoString, LEN(__ANSI_F8_3))) = __ANSI_F8_3 THEN
ANSI_GetFontHeight = 8
ELSEIF UCASE$(LEFT$(sauceTypeInfoString, LEN(__ANSI_F8_5))) = __ANSI_F8_5 THEN
ANSI_GetFontHeight = 8
ELSEIF UCASE$(LEFT$(sauceTypeInfoString, LEN(__ANSI_F8_4))) = __ANSI_F8_4 THEN
ANSI_GetFontHeight = 8
ELSE
ANSI_GetFontHeight = 16
END IF
END FUNCTION
FUNCTION ANSI_GetWidth& (sauce AS SAUCEType)
DIM w AS LONG: w = SAUCE_GetTypeInfoLong1(sauce)
IF w THEN
ANSI_GetWidth = w
ELSE
ANSI_GetWidth = 80
END IF
END FUNCTION
FUNCTION ANSI_GetHeight& (sauce AS SAUCEType)
DIM h AS LONG: h = SAUCE_GetTypeInfoLong2(sauce)
IF h THEN
ANSI_GetHeight = h
ELSE
ANSI_GetHeight = 25
END IF
END FUNCTION
'$INCLUDE:'GraphicOps.bas'
'$INCLUDE:'SAUCE.bas'