summaryrefslogtreecommitdiff
path: root/cat-bdf-fonts.sh
blob: d2afc51a99d73be313cf25cd8e33d767d4138df6 (plain)
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
#!/bin/sh

########################################################################
## cat-bdf-fonts.sh:  a script to concatenate the contents of the given
##                    BDF font into one BDF font.
## created   1998-10-10 00:28 Jim Knoble <jmknoble@pobox.com>
## autodate: 1999-Aug-17 00:46
########################################################################

if [ -n "${DEBUG}" ]; then
    set -x
fi

PrintHelp() {
    cat <<EndOfHelp
    
  Usage: $0 [-o <outfile> [-b]] [-p <font>] \\
         [-a <add-style>] [-r <registry>] [-e <encoding>] \\
         <font> [<font> ...]

  Concatenate the given BDF-format fonts to standard output.
  
  Options:
  
    -o <outfile>    Concatenate to <outfile> instead of stdout.
    
    -b              Make a backup of <outfile> in <outfile>~.
    
    -p <font>       Include the preamble of <font> as the preamble for
                    the output font.  If not specified, use the preamble
                    from the first font given on the command line.

    -n <name>       Set the value of the FAMILY_NAME field in the
                    output font to <name>.

    -a <add-style>  Set the value of the ADD_STYLE_NAME field in the
                    output font to <add-style>.

    -r <registry>   Set the value of the CHARSET_REGISTRY field in the
                    output font to <registry>.

    -e <encoding>   Set the value of the CHARSET_ENCODING field in the
                    output font to <encoding>.

EndOfHelp
}

########################################################################
## Some useful variables and functions
SuffixBackup="~"
SuffixBdf=".bdf"
OutputFile="font${SuffixBdf}"
PreambleFont=""
MakeBackup=0

CharPattern="^(STARTCHAR|ENCODING|SWIDTH|DWIDTH|BBX|BITMAP|ENDCHAR|[0-9A-F][0-9A-F]*$)"
EndPattern="ENDFONT"

GetBdfPreamble() {
    egrep -v "${CharPattern}" | grep -v "${EndPattern}"
}
GetBdfChars() {
    egrep "${CharPattern}"
}
## Assign a value to a font property
## syntax:  cat <bdffile> | FontProp <num-preceding> <name> <value>
## For example:  cat blah.bdf | FontProp 12 CHARSET_REGISTRY ISO8859
FontProp() {
    local n
    local name
    local val
    n=$1
    name=$2
    val="$3"
    sed -e 's/^FONT[[:space:]][[:space:]]*\(\(-[^-]*\)\{'"${n}"'\}\)-[^-]*\(.*\)$/FONT \1-'"${val}"'\3/' \
        -e 's/^'"${name}"'[[:space:]][[:space:]]*".*".*$/'"${name}"' "'"${val}"'"/'
}
MakeFamilyName() {
    FontProp 1 FAMILY_NAME "$1"
}
MakeAddStyle() {
    FontProp 5 ADD_STYLE_NAME "$1"
}
MakeRegistry() {
    FontProp 12 CHARSET_REGISTRY "$1"
}
MakeEncoding() {
    FontProp 13 CHARSET_ENCODING "$1"
}

########################################################################
## Process command-line options
while [ -n "$*" ]; do
    case "$1" in
        -h|--help)
            PrintHelp
            exit 1
            ;;
        -b|--backup)
            shift
	    MakeBackup=1
	    ;;
        -o|--output)
            shift
	    OutputFile="$1"
	    shift
	    ;;
        -p|--preamble)
	    shift
	    PreambleFont="$1"
	    shift
	    ;;
        -a|--add-style)
	    shift
	    AddStyle="$1"
	    shift
	    ;;
        -n|--name)
	    shift
	    Name="$1"
	    shift
	    ;;
        -r|--registry)
	    shift
	    CharsetRegistry="$1"
	    shift
	    ;;
        -e|--encoding)
	    shift
	    CharsetEncoding="$1"
	    shift
	    ;;
        *)
	    break
	    ;;
    esac
done

if [ -z "$*" ]; then
    echo "`basename $0`: No BDF fonts specified."
    exit 1
fi

if [ -z "${PreambleFont}" ]; then
    PreambleFont="$1"
fi
BackupFile="${OutputFile}${SuffixBackup}"

########################################################################
## Create the preamble
PropFilters=""
if [ -n "${Name}" ]; then
    PropFilters="${PropFilters} | MakeFamilyName '${Name}'"
fi
if [ -n "${AddStyle}" ]; then
    PropFilters="${PropFilters} | MakeAddStyle '${AddStyle}'"
fi
if [ -n "${CharsetRegistry}" ]; then
    PropFilters="${PropFilters} | MakeRegistry '${CharsetRegistry}'"
fi
if [ -n "${CharsetEncoding}" ]; then
    PropFilters="${PropFilters} | MakeEncoding '${CharsetEncoding}'"
fi

TmpFile="`mktemp tmpfont.XXXXXX`" || exit 1
eval "cat '${PreambleFont}' | GetBdfPreamble ${PropFilters} >'${TmpFile}'"

## Now add the characters from the fonts in order
for FontName in "$@"; do
    cat "${FontName}" | GetBdfChars >>"${TmpFile}"
done
echo ENDFONT >>"${TmpFile}"

## Finally, figure out how many characters are in the font
NumChars=`echo -n \`cat "${TmpFile}" | grep '^ENCODING' | wc -l\``

if [ ${MakeBackup} -gt 0 -a -e "${OutputFile}" ]; then
    cp -pf "${OutputFile}" "${BackupFile}"
fi

cat "${TmpFile}" | \
    sed -e 's/^CHARS[[:space:]][[:space:]]*.*$/CHARS '"${NumChars}"'/' \
    >${OutputFile}
rm -f "${TmpFile}"

## -------- End of file --------