dev-qt/qttest: stable 5.14.2 for ppc, bug #719732
[gentoo.git] / eclass / font.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: font.eclass
5 # @MAINTAINER:
6 # fonts@gentoo.org
7 # @SUPPORTED_EAPIS: 5 6 7
8 # @BLURB: Eclass to make font installation uniform
9
10 case ${EAPI:-0} in
11         [56]) inherit eutils ;;
12         7) ;;
13         *) die "EAPI ${EAPI} is not supported by font.eclass." ;;
14 esac
15
16 if [[ ! ${_FONT_ECLASS} ]]; then
17
18 EXPORT_FUNCTIONS pkg_setup src_install pkg_postinst pkg_postrm
19
20 # @ECLASS-VARIABLE: FONT_SUFFIX
21 # @DEFAULT_UNSET
22 # @REQUIRED
23 # @DESCRIPTION:
24 # Space delimited list of font suffixes to install.
25 FONT_SUFFIX=${FONT_SUFFIX:-}
26
27 # @ECLASS-VARIABLE: FONT_S
28 # @DEFAULT_UNSET
29 # @DESCRIPTION:
30 # Directory containing the fonts.  If unset, ${S} is used instead.
31 # Can also be an array of several directories.
32
33 # @ECLASS-VARIABLE: FONT_PN
34 # @DESCRIPTION:
35 # Font name (ie. last part of FONTDIR).
36 FONT_PN=${FONT_PN:-${PN}}
37
38 # @ECLASS-VARIABLE: FONTDIR
39 # @DESCRIPTION:
40 # Full path to installation directory.
41 FONTDIR=${FONTDIR:-/usr/share/fonts/${FONT_PN}}
42
43 # @ECLASS-VARIABLE: FONT_CONF
44 # @DEFAULT_UNSET
45 # @DESCRIPTION:
46 # Array containing fontconfig conf files to install.
47 FONT_CONF=( "" )
48
49 # @ECLASS-VARIABLE: DOCS
50 # @DEFAULT_UNSET
51 # @DESCRIPTION:
52 # Space delimited list of docs to install.
53 # We always install these:
54 # COPYRIGHT README{,.txt} NEWS AUTHORS BUGS ChangeLog FONTLOG.txt
55 DOCS=${DOCS:-}
56
57 if [[ ${CATEGORY}/${PN} != media-fonts/encodings ]]; then
58         IUSE="X"
59         DEPEND="X? (
60                         >=x11-apps/mkfontscale-1.2.0
61                         media-fonts/encodings
62         )"
63         RDEPEND=""
64 fi
65
66 # @FUNCTION: font_xfont_config
67 # @DESCRIPTION:
68 # Generate Xorg font files (mkfontscale/mkfontdir).
69 font_xfont_config() {
70         local dir_name
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"
77                 mkfontdir \
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
82         fi
83 }
84
85 # @FUNCTION: font_fontconfig
86 # @DESCRIPTION:
87 # Install fontconfig conf files given in FONT_CONF.
88 font_fontconfig() {
89         local conffile
90         if [[ -n ${FONT_CONF[@]} ]]; then
91                 insinto /etc/fonts/conf.avail/
92                 for conffile in "${FONT_CONF[@]}"; do
93                         [[ -e ${conffile} ]] && doins "${conffile}"
94                 done
95         fi
96 }
97
98 # @FUNCTION: font_cleanup_dirs
99 # @DESCRIPTION:
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
105
106         ebegin "Cleaning up font directories"
107         while read -d $'\0' -r; do
108                 candidate=false
109                 otherfile=false
110                 for f in "${d}"/*; do
111                         generated=false
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
116                                 generated=true
117                                 break
118                         fi
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
126                 done
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}"
135                                 fi
136                         done
137                         # if there's nothing left remove the directory
138                         find "${d}" -maxdepth 0 -type d -empty -delete || eerror "failed to purge ${d}"
139                 fi
140         done < <(find -L "${EROOT%/}"/usr/share/fonts/ -type d -print0)
141         eend 0
142 }
143
144 # @FUNCTION: font_pkg_setup
145 # @DESCRIPTION:
146 # The font pkg_setup function.
147 # Collision protection
148 font_pkg_setup() {
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"
153         fi
154 }
155
156 # @FUNCTION: font_src_install
157 # @DESCRIPTION:
158 # The font src_install function.
159 font_src_install() {
160         local dir suffix commondoc
161
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
168                                 doins *.${suffix}
169                         done
170                         font_xfont_config "${dir}"
171                         popd > /dev/null || die
172                 done
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
181                                 doins *.${suffix}
182                         done
183                         font_xfont_config "${dir}"
184                         popd > /dev/null || die
185                 done
186         else
187                 pushd "${FONT_S:-${S}}" > /dev/null \
188                         || die "pushd ${FONT_S:-${S}} failed"
189                 insinto "${FONTDIR}"
190                 for suffix in ${FONT_SUFFIX}; do
191                         doins *.${suffix}
192                 done
193                 font_xfont_config
194                 popd > /dev/null || die
195         fi
196
197         font_fontconfig
198
199         einstalldocs
200
201         # install common docs
202         for commondoc in COPYRIGHT FONTLOG.txt; do
203                 [[ -s ${commondoc} ]] && dodoc ${commondoc}
204         done
205 }
206
207 # @FUNCTION: _update_fontcache
208 # @DESCRIPTION:
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"
214
215         if [[ -z ${ROOT%/} ]] ; then
216                 if has_version media-libs/fontconfig ; then
217                         ebegin "Updating global fontcache"
218                         fc-cache -fs
219                         if ! eend $? ; then
220                                 die "failed to update global fontcache"
221                         fi
222                 else
223                         einfo "Skipping fontcache update (media-libs/fontconfig not installed)"
224                 fi
225         else
226                 einfo "Skipping fontcache update (ROOT != /)"
227         fi
228 }
229
230 # @FUNCTION: font_pkg_postinst
231 # @DESCRIPTION:
232 # The font pkg_postinst function.
233 font_pkg_postinst() {
234         if [[ -n ${FONT_CONF[@]} ]]; then
235                 local conffile
236                 elog "The following fontconfig configuration files have been installed:"
237                 elog
238                 for conffile in "${FONT_CONF[@]}"; do
239                         [[ -e "${EROOT%/}"/etc/fonts/conf.avail/${conffile##*/} ]] &&
240                                 elog "  ${conffile##*/}"
241                 done
242                 elog
243                 elog "Use \`eselect fontconfig\` to enable/disable them."
244         fi
245
246         _update_fontcache
247 }
248
249 # @FUNCTION: font_pkg_postrm
250 # @DESCRIPTION:
251 # The font pkg_postrm function.
252 font_pkg_postrm() {
253         font_cleanup_dirs
254         _update_fontcache
255 }
256
257 _FONT_ECLASS=1
258 fi