#!/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 .
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
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 \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 "${@}"