1 # Copyright 1999-2019 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 # Space delimited list of directories containing the fonts.
31 FONT_S=${FONT_S:-${S}}
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 set -- ${FONT_S:-${S}}
163 if [[ $# -gt 1 ]]; then
164 # if we have multiple FONT_S elements then we want to recreate the dir
166 for dir in ${FONT_S}; do
167 pushd "${dir}" > /dev/null
168 insinto "${FONTDIR}/${dir//${S}/}"
169 for suffix in ${FONT_SUFFIX}; do
172 font_xfont_config "${dir}"
176 pushd "${FONT_S}" > /dev/null
178 for suffix in ${FONT_SUFFIX}; do
189 # install common docs
190 for commondoc in COPYRIGHT FONTLOG.txt; do
191 [[ -s ${commondoc} ]] && dodoc ${commondoc}
195 # @FUNCTION: _update_fontcache
197 # Updates fontcache if !prefix and media-libs/fontconfig installed
198 _update_fontcache() {
199 # unreadable font files = fontconfig segfaults
200 find "${EROOT%/}"/usr/share/fonts/ -type f '!' -perm 0644 \
201 -exec chmod -v 0644 2>/dev/null {} + || die "failed to fix font files perms"
203 if [[ -z ${ROOT%/} ]] ; then
204 if has_version media-libs/fontconfig ; then
205 ebegin "Updating global fontcache"
208 die "failed to update global fontcache"
211 einfo "Skipping fontcache update (media-libs/fontconfig not installed)"
214 einfo "Skipping fontcache update (ROOT != /)"
218 # @FUNCTION: font_pkg_postinst
220 # The font pkg_postinst function.
221 font_pkg_postinst() {
222 if [[ -n ${FONT_CONF[@]} ]]; then
224 elog "The following fontconfig configuration files have been installed:"
226 for conffile in "${FONT_CONF[@]}"; do
227 [[ -e "${EROOT%/}"/etc/fonts/conf.avail/${conffile##*/} ]] &&
228 elog " ${conffile##*/}"
231 elog "Use \`eselect fontconfig\` to enable/disable them."
237 # @FUNCTION: font_pkg_postrm
239 # The font pkg_postrm function.