-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin2psf.bas
122 lines (100 loc) · 4.61 KB
/
bin2psf.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
'-----------------------------------------------------------------------------------------------------------------------
' Tiny tool to convert raw VGA character ROM to PSF1 (PC Screen Font v1) format
' See https://github.com/spacerace/romfont to learn more about VGA ROM fonts or character ROM
' See https://www.win.tue.nl/~aeb/linux/kbd/font-formats-1.html to learn about the PSF format
'
' Copyright (c) 2024 Samuel Gomes
'-----------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------
' METACOMMANDS
'-----------------------------------------------------------------------------------------------------------------------
$CONSOLE:ONLY
'-----------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------
' HEADER FILES
'-----------------------------------------------------------------------------------------------------------------------
'$INCLUDE:'include/VGAFont.bi'
'-----------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------
' PROGRAM ENTRY POINT
'-----------------------------------------------------------------------------------------------------------------------
' Change to the directory specified by the environment
CHDIR _STARTDIR$
' If there are no command line parameters just show some info and exit
IF _COMMANDCOUNT < 1 THEN
PRINT
PRINT "Bin2PSF: Converts raw VGA ROM fonts to PSF1 (PC Screen Font v1)"
PRINT
PRINT "Copyright (c) 2024 Samuel Gomes"
PRINT
PRINT "https://github.com/a740g"
PRINT
PRINT "Usage: bin2psf [filespec]"
PRINT
PRINT "Note:"
PRINT " * This will create filespec.psf"
PRINT " * Bulk convert files using wildcards"
PRINT " * If filespec.psf already exists, then it will not be overwritten"
PRINT " * The tools assumes that the file includes all 256 glyphs"
PRINT
SYSTEM
END IF
PRINT
DIM AS LONG i, h
' Convert all files requested
FOR i = 1 TO _COMMANDCOUNT
PRINT "Attempting to convert "; COMMAND$(i); " ... ";
h = ConvertBin2PSF(COMMAND$(i), COMMAND$(i) + ".psf")
IF h > 0 THEN
PRINT "8 x"; h; "done!"
ELSE
PRINT "failed!"
END IF
NEXT
SYSTEM
'-----------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------
' FUNCTIONS AND SUBROUTINES
'-----------------------------------------------------------------------------------------------------------------------
FUNCTION ConvertBin2PSF& (sBinFileName AS STRING, sPSFFileName AS STRING)
IF _FILEEXISTS(sBinFileName) AND NOT _FILEEXISTS(sPSFFileName) THEN
' Open the raw ROM font file
DIM binFileHandle AS LONG
binFileHandle = FREEFILE
OPEN sBinFileName FOR BINARY ACCESS READ AS binFileHandle
DIM h AS LONG
' Get and store the raw file size
h = LOF(binFileHandle)
' Basic check: The raw font should be completely divisible by 256
IF h MOD 256 <> 0 _ORELSE h = 0 THEN
CLOSE binFileHandle
EXIT FUNCTION
END IF
' Open the PSF file
DIM psfFilehandle AS LONG
psfFilehandle = FREEFILE
OPEN sPSFFileName FOR BINARY ACCESS WRITE AS psfFilehandle
' Calculate font height
h = h \ 256
DIM buffer AS STRING
' Write the magic ID
buffer = CHR$(__PSF1_MAGIC0) + CHR$(__PSF1_MAGIC1)
PUT psfFilehandle, , buffer
' Write mode (just a NULL)
buffer = CHR$(NULL)
PUT psfFilehandle, , buffer
' Write charsize
buffer = CHR$(h)
PUT psfFilehandle, , buffer
' Read the font data
buffer = INPUT$(h * 256, binFileHandle)
' Write the font data
PUT psfFilehandle, , buffer
' Close all files
CLOSE psfFilehandle, binFileHandle
' Return the font height
ConvertBin2PSF = h
END IF
END FUNCTION
'-----------------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------------