summaryrefslogtreecommitdiffstats
path: root/scripts/gen-mplus-patch.sh
blob: af7634f0ccd65c6fc79a49654df6a847c36480ae (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/sh
#
# Generate a patch to add M+ fbcon fonts to Linux(-libre)
#
# Copyright (C) 2019  Patrick McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -eu

VARIANTS='
	1m  regular 16 22
	1m  bold    16 22
	1mn regular 16 22
	1mn bold    16 22
	'
LF='
'

backup_orig()
{
	local f=

	for f in include/linux/font.h lib/fonts/Kconfig lib/fonts/Makefile \
			lib/fonts/fonts.c; do
		if ! [ -e "tmp/src/${f}.orig" ]; then
			mv "tmp/src/${f}" "tmp/src/${f}.orig"
		fi
	done
}

edit_font_h()
{
	local state=
	local i=
	local line=
	local ft=

	exec 3>'tmp/src/include/linux/font.h'
	state='want_defs'
	i=0
	while IFS='' read -r line; do
		if [ x"${state}" = x'want_defs' ] || [ x"${state}" = x'defs' ]
		then
			case "${line}" in '#define '*'_IDX'*)
				state='defs'
				printf '%s\n' "${line}" 1>&3
				: $((i += 1))
				continue
			esac
		fi
		if [ x"${state}" = x'defs' ]; then  # End of defines
			while read fc wt wd ht; do
				[ -z "${fc}" ] && continue
				ft="MPLUS_$(printf '%s_%s' "${fc}" "${wt}" | \
					tr '[a-z]' '[A-Z]')_${wd}x${ht}_IDX"
				printf '#define %s\t%d\n' "${ft}" ${i} 1>&3
				: $((i += 1))
			done <<-EOF
				${VARIANTS}
				EOF
			state='want_decls'
		fi
		if [ x"${state}" = x'want_decls' ]; then
			case "${line}" in *'struct font_desc'*)
				state='decls'
			esac
		fi
		if [ x"${state}" = x'decls' ]; then
			case "${line}" in *';')
				printf '%s\n' "${line%;}" 1>&3
				while read fc wt wd ht; do
					[ -z "${fc}" ] && continue
					printf ',\t\t\tfont_mplus_%s_%s'"$(: \
						)"'_%dx%d' "${fc}" "${wt}" \
						"${wd}" "${ht}" 1>&3
				done <<-EOF
					${VARIANTS}
					EOF
				printf ';\n' 1>&3
				state='end'
				continue
			esac
			printf '%s\n' "${line}" 1>&3
		fi
		printf '%s\n' "${line}" 1>&3
	done 0<'tmp/src/include/linux/font.h.orig'

	if ! [ x"${state}" = x'end' ]; then
		printf 'Error editing include/linux/font.h\n' 1>&2
		return 1
	fi

	return 0
}

edit_kconfig()
{
}

edit_makefile()
{
}

edit_fonts_c()
{
}


gen_headers()
{
	local mplus_dir="${1}"
	shift 1
	local mplus_ver=

	mplus_ver="$(tr '\0' '\n' <"${mplus_dir}/mplus-1m-regular.ttf" | \
		sed -n 's/^Version 1.//p')"
	cat <<-EOF
	Author: Patrick McDermott <patrick.mcdermott@libiquity.com>
	Subject: Add M+ fonts

	Generated from M+ version ${mplus_ver} by ${0}
	EOF
	return 0
}

gen_diff()
{
	local headers="${1}"
	shift 1
	local files=
	local fc=
	local wt=
	local wd=
	local ht=
	local f=
	local diff=
	local a=
	local b=

	files="include/linux/font.h${LF}lib/fonts/Kconfig${LF}"
	files="${files}lib/fonts/Makefile${LF}"
	while read fc wt wd ht; do
		[ -z "${fc}" ] && continue
		f="lib/fonts/font_mplus_${fc}_${wt}_${wd}x${ht}.c"
		files="${files}${f}${LF}"
	done <<-EOF
		${VARIANTS}
		EOF
	files="${files}lib/fonts/fonts.c${LF}"

	diff=''
	for f in ${files}; do
		a="tmp/src/${f}"
		b="tmp/src/${f}.orig"
		case "${f}" in lib/fonts/font_*.c) a='/dev/null';; esac
		diff="${diff}diff -Naurp a/${f} b/${f}${LF}"
		diff="${diff}--- a/${f}${LF}"
		diff="${diff}+++ a/${f}${LF}"
		diff="${diff}$(diff -up "${a}" "${b}" | tail -n +3)${LF}"
	done
	diff="${headers}${LF}---${LF}$(printf '%s' "${diff}" | \
		diffstat)${LF}${diff}"

	return 0
}

main()
{
	local mplus_dir=
	local bdf2fbcon=

	if [ ${#} -ne 2 ]; then
		printf 'Usage: %s <mplus-dir> <bdf2fbcon>\n' "${0}" 1>&2
		return 1
	fi
	mplus_dir="${1}"
	bdf2fbcon="${2}"
	shift 2

	conv_fonts "${mplus_dir}" "${bdf2fbcon}"
	backup_orig
	edit_font_h
	edit_kconfig
	edit_makefile
	edit_fonts_c
	gen_diff "$(gen_headers "${mplus_dir}")"

	return 0
}

main "${@}"