app-eselect/eselect-postgresql: [QA] Fix BadHomepage
[gentoo.git] / eclass / font.eclass
1 # Copyright 1999-2019 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 # @REQUIRED
29 # @DESCRIPTION:
30 # Space delimited list of directories containing the fonts.
31 FONT_S=${FONT_S:-${S}}
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         set -- ${FONT_S:-${S}}
163         if [[ $# -gt 1 ]]; then
164                 # if we have multiple FONT_S elements then we want to recreate the dir
165                 # structure
166                 for dir in ${FONT_S}; do
167                         pushd "${dir}" > /dev/null
168                         insinto "${FONTDIR}/${dir//${S}/}"
169                         for suffix in ${FONT_SUFFIX}; do
170                                 doins *.${suffix}
171                         done
172                         font_xfont_config "${dir}"
173                         popd > /dev/null
174                 done
175         else
176                 pushd "${FONT_S}" > /dev/null
177                 insinto "${FONTDIR}"
178                 for suffix in ${FONT_SUFFIX}; do
179                         doins *.${suffix}
180                 done
181                 font_xfont_config
182                 popd > /dev/null
183         fi
184
185         font_fontconfig
186
187         einstalldocs
188
189         # install common docs
190         for commondoc in COPYRIGHT FONTLOG.txt; do
191                 [[ -s ${commondoc} ]] && dodoc ${commondoc}
192         done
193 }
194
195 # @FUNCTION: _update_fontcache
196 # @DESCRIPTION:
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"
202
203         if [[ -z ${ROOT%/} ]] ; then
204                 if has_version media-libs/fontconfig ; then
205                         ebegin "Updating global fontcache"
206                         fc-cache -fs
207                         if ! eend $? ; then
208                                 die "failed to update global fontcache"
209                         fi
210                 else
211                         einfo "Skipping fontcache update (media-libs/fontconfig not installed)"
212                 fi
213         else
214                 einfo "Skipping fontcache update (ROOT != /)"
215         fi
216 }
217
218 # @FUNCTION: font_pkg_postinst
219 # @DESCRIPTION:
220 # The font pkg_postinst function.
221 font_pkg_postinst() {
222         if [[ -n ${FONT_CONF[@]} ]]; then
223                 local conffile
224                 elog "The following fontconfig configuration files have been installed:"
225                 elog
226                 for conffile in "${FONT_CONF[@]}"; do
227                         [[ -e "${EROOT%/}"/etc/fonts/conf.avail/${conffile##*/} ]] &&
228                                 elog "  ${conffile##*/}"
229                 done
230                 elog
231                 elog "Use \`eselect fontconfig\` to enable/disable them."
232         fi
233
234         _update_fontcache
235 }
236
237 # @FUNCTION: font_pkg_postrm
238 # @DESCRIPTION:
239 # The font pkg_postrm function.
240 font_pkg_postrm() {
241         font_cleanup_dirs
242         _update_fontcache
243 }
244
245 _FONT_ECLASS=1
246 fi