1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
7 # @SUPPORTED_EAPIS: 5 6 7
8 # @BLURB: Eclass to make font installation uniform
11 [56]) inherit eutils ;;
13 *) die "EAPI ${EAPI} is not supported by font.eclass." ;;
16 if [[ ! ${_FONT_ECLASS} ]]; then
18 EXPORT_FUNCTIONS pkg_setup src_install pkg_postinst pkg_postrm
20 # @ECLASS-VARIABLE: FONT_SUFFIX
24 # Space delimited list of font suffixes to install.
25 FONT_SUFFIX=${FONT_SUFFIX:-}
27 # @ECLASS-VARIABLE: FONT_S
30 # Directory containing the fonts. If unset, ${S} is used instead.
31 # Can also be an array of several directories.
33 # @ECLASS-VARIABLE: FONT_PN
35 # Font name (ie. last part of FONTDIR).
36 FONT_PN=${FONT_PN:-${PN}}
38 # @ECLASS-VARIABLE: FONTDIR
40 # Full path to installation directory.
41 FONTDIR=${FONTDIR:-/usr/share/fonts/${FONT_PN}}
43 # @ECLASS-VARIABLE: FONT_CONF
46 # Array containing fontconfig conf files to install.
49 # @ECLASS-VARIABLE: DOCS
52 # Space delimited list of docs to install.
53 # We always install these:
54 # COPYRIGHT README{,.txt} NEWS AUTHORS BUGS ChangeLog FONTLOG.txt
57 if [[ ${CATEGORY}/${PN} != media-fonts/encodings ]]; then
60 >=x11-apps/mkfontscale-1.2.0
66 # @FUNCTION: font_xfont_config
68 # Generate Xorg font files (mkfontscale/mkfontdir).
71 if in_iuse X && use X ; then
72 dir_name="${1:-${FONT_PN}}"
73 rm -f "${ED%/}/${FONTDIR}/${1//${S}/}"/{fonts.{dir,scale},encodings.dir} \
74 || die "failed to prepare ${FONTDIR}/${1//${S}/}"
75 einfo "Creating fonts.scale & fonts.dir in ${dir_name##*/}"
76 mkfontscale "${ED%/}/${FONTDIR}/${1//${S}/}" || eerror "failed to create fonts.scale"
78 -e ${EPREFIX}/usr/share/fonts/encodings \
79 -e ${EPREFIX}/usr/share/fonts/encodings/large \
80 "${ED%/}/${FONTDIR}/${1//${S}/}" || eerror "failed to create fonts.dir"
81 [[ -e fonts.alias ]] && doins fonts.alias
85 # @FUNCTION: font_fontconfig
87 # Install fontconfig conf files given in FONT_CONF.
90 if [[ -n ${FONT_CONF[@]} ]]; then
91 insinto /etc/fonts/conf.avail/
92 for conffile in "${FONT_CONF[@]}"; do
93 [[ -e ${conffile} ]] && doins "${conffile}"
98 # @FUNCTION: font_cleanup_dirs
100 # Remove font directories containing only generated files.
101 font_cleanup_dirs() {
102 local genfiles="encodings.dir fonts.alias fonts.cache-1 fonts.dir fonts.scale"
103 # fonts.alias isn't generated but it's a special case (see below).
104 local d f g generated candidate otherfile
106 ebegin "Cleaning up font directories"
107 while read -d $'\0' -r; do
110 for f in "${d}"/*; do
112 # make sure this is a file and not a subdir
113 [[ -e ${f} || -L ${f} ]] || continue
114 if has ${f##*/} ${genfiles}; then
115 # this is a generated file
119 # if the file is a generated file then we know this is a font dir (as
120 # opposed to something like encodings or util) and a candidate for
121 # removal. if it's not generated then it's an "otherfile".
122 ${generated} && candidate=true || otherfile=true
123 # if the directory is both a candidate for removal and contains at
124 # least one "otherfile" then don't remove it.
125 [[ ${candidate} == ${otherfile} ]] && break
127 # if in the end we only have generated files, purge the directory.
128 if [[ ${candidate} == true && ${otherfile} == false ]]; then
129 # we don't want to remove fonts.alias files that were installed by
130 # media-fonts/font-alias. any other fonts.alias files will have
131 # already been unmerged with their packages.
132 for g in ${genfiles}; do
133 if [[ ${g} != fonts.alias && ( -e ${d}/${g} || -L ${d}/${g} ) ]] ; then
134 rm "${d}"/${g} || eerror "failed to remove ${d}/${g}"
137 # if there's nothing left remove the directory
138 find "${d}" -maxdepth 0 -type d -empty -delete || eerror "failed to purge ${d}"
140 done < <(find -L "${EROOT%/}"/usr/share/fonts/ -type d -print0)
144 # @FUNCTION: font_pkg_setup
146 # The font pkg_setup function.
147 # Collision protection
149 # make sure we get no collisions
150 # setup is not the nicest place, but preinst doesn't cut it
151 if [[ -e "${EROOT%/}/${FONTDIR}/fonts.cache-1" ]] ; then
152 rm "${EROOT%/}/${FONTDIR}/fonts.cache-1" || die "failed to remove fonts.cache-1"
156 # @FUNCTION: font_src_install
158 # The font src_install function.
160 local dir suffix commondoc
162 if [[ $(declare -p FONT_S 2>/dev/null) == "declare -a"* ]]; then
163 # recreate the directory structure if FONT_S is an array
164 for dir in "${FONT_S[@]}"; do
165 pushd "${dir}" > /dev/null || die "pushd ${dir} failed"
166 insinto "${FONTDIR}/${dir#"${S}"}"
167 for suffix in ${FONT_SUFFIX}; do
170 font_xfont_config "${dir}"
171 popd > /dev/null || die
173 elif [[ ${FONT_S/[[:space:]]} != "${FONT_S}" ]]; then
174 # backwards compatibility code, can be removed after 2021-02-14
175 eqawarn "Using a space-separated list for FONT_S is deprecated."
176 eqawarn "Use a bash array instead if there are multiple directories."
177 for dir in ${FONT_S}; do
178 pushd "${dir}" > /dev/null || die "pushd ${dir} failed"
179 insinto "${FONTDIR}/${dir//${S}/}"
180 for suffix in ${FONT_SUFFIX}; do
183 font_xfont_config "${dir}"
184 popd > /dev/null || die
187 pushd "${FONT_S:-${S}}" > /dev/null \
188 || die "pushd ${FONT_S:-${S}} failed"
190 for suffix in ${FONT_SUFFIX}; do
194 popd > /dev/null || die
201 # install common docs
202 for commondoc in COPYRIGHT FONTLOG.txt; do
203 [[ -s ${commondoc} ]] && dodoc ${commondoc}
207 # @FUNCTION: _update_fontcache
209 # Updates fontcache if !prefix and media-libs/fontconfig installed
210 _update_fontcache() {
211 # unreadable font files = fontconfig segfaults
212 find "${EROOT%/}"/usr/share/fonts/ -type f '!' -perm 0644 \
213 -exec chmod -v 0644 2>/dev/null {} + || die "failed to fix font files perms"
215 if [[ -z ${ROOT%/} ]] ; then
216 if has_version media-libs/fontconfig ; then
217 ebegin "Updating global fontcache"
220 die "failed to update global fontcache"
223 einfo "Skipping fontcache update (media-libs/fontconfig not installed)"
226 einfo "Skipping fontcache update (ROOT != /)"
230 # @FUNCTION: font_pkg_postinst
232 # The font pkg_postinst function.
233 font_pkg_postinst() {
234 if [[ -n ${FONT_CONF[@]} ]]; then
236 elog "The following fontconfig configuration files have been installed:"
238 for conffile in "${FONT_CONF[@]}"; do
239 [[ -e "${EROOT%/}"/etc/fonts/conf.avail/${conffile##*/} ]] &&
240 elog " ${conffile##*/}"
243 elog "Use \`eselect fontconfig\` to enable/disable them."
249 # @FUNCTION: font_pkg_postrm
251 # The font pkg_postrm function.