app-admin/lastpass-cli: serve man from devspace #654846
[gentoo.git] / eclass / font.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: font.eclass
5 # @MAINTAINER:
6 # fonts@gentoo.org
7 # @BLURB: Eclass to make font installation uniform
8
9 inherit eutils
10
11 EXPORT_FUNCTIONS pkg_setup src_install pkg_postinst pkg_postrm
12
13 # @ECLASS-VARIABLE: FONT_SUFFIX
14 # @DEFAULT_UNSET
15 # @REQUIRED
16 # @DESCRIPTION:
17 # Space delimited list of font suffixes to install.
18 FONT_SUFFIX=${FONT_SUFFIX:-}
19
20 # @ECLASS-VARIABLE: FONT_S
21 # @REQUIRED
22 # @DESCRIPTION:
23 # Space delimited list of directories containing the fonts.
24 FONT_S=${FONT_S:-${S}}
25
26 # @ECLASS-VARIABLE: FONT_PN
27 # @DESCRIPTION:
28 # Font name (ie. last part of FONTDIR).
29 FONT_PN=${FONT_PN:-${PN}}
30
31 # @ECLASS-VARIABLE: FONTDIR
32 # @DESCRIPTION:
33 # Full path to installation directory.
34 FONTDIR=${FONTDIR:-/usr/share/fonts/${FONT_PN}}
35
36 # @ECLASS-VARIABLE: FONT_CONF
37 # @DEFAULT_UNSET
38 # @DESCRIPTION:
39 # Array containing fontconfig conf files to install.
40 FONT_CONF=( "" )
41
42 # @ECLASS-VARIABLE: DOCS
43 # @DEFAULT_UNSET
44 # @DESCRIPTION:
45 # Space delimited list of docs to install.
46 # We always install these:
47 # COPYRIGHT README{,.txt} NEWS AUTHORS BUGS ChangeLog FONTLOG.txt
48 DOCS=${DOCS:-}
49
50 IUSE="X"
51
52 DEPEND="X? (
53                 x11-apps/mkfontdir
54                 media-fonts/encodings
55         )"
56 RDEPEND=""
57
58 # @FUNCTION: font_xfont_config
59 # @DESCRIPTION:
60 # Generate Xorg font files (mkfontscale/mkfontdir).
61 font_xfont_config() {
62         local dir_name
63         if has X ${IUSE//+} && use X ; then
64                 dir_name="${1:-${FONT_PN}}"
65                 ebegin "Creating fonts.scale & fonts.dir in ${dir_name##*/}"
66                 rm -f "${ED}${FONTDIR}/${1//${S}/}"/{fonts.{dir,scale},encodings.dir}
67                 mkfontscale "${ED}${FONTDIR}/${1//${S}/}"
68                 mkfontdir \
69                         -e ${EPREFIX}/usr/share/fonts/encodings \
70                         -e ${EPREFIX}/usr/share/fonts/encodings/large \
71                         "${ED}${FONTDIR}/${1//${S}/}"
72                 eend $?
73                 if [[ -e fonts.alias ]] ; then
74                         doins fonts.alias
75                 fi
76         fi
77 }
78
79 # @FUNCTION: font_fontconfig
80 # @DESCRIPTION:
81 # Install fontconfig conf files given in FONT_CONF.
82 font_fontconfig() {
83         local conffile
84         if [[ -n ${FONT_CONF[@]} ]]; then
85                 insinto /etc/fonts/conf.avail/
86                 for conffile in "${FONT_CONF[@]}"; do
87                         [[ -e  ${conffile} ]] && doins ${conffile}
88                 done
89         fi
90 }
91
92 # @FUNCTION: font_cleanup_dirs
93 # @DESCRIPTION:
94 # Remove font directories containing only generated files.
95 font_cleanup_dirs() {
96         local genfiles="encodings.dir fonts.alias fonts.cache-1 fonts.dir fonts.scale"
97         # fonts.alias isn't generated but it's a special case (see below).
98         local d f g generated candidate otherfile
99
100         ebegin "Cleaning up font directories"
101         find -L "${EROOT}"usr/share/fonts/ -type d -print0 | while read -d $'\0' d; do
102                 candidate=false
103                 otherfile=false
104                 for f in "${d}"/*; do
105                         generated=false
106                         # make sure this is a file and not a subdir
107                         [[ -e ${f} || -L ${f} ]] || continue
108                         for g in ${genfiles}; do
109                                 if [[ ${f##*/} == ${g} ]]; then
110                                         # this is a generated file
111                                         generated=true
112                                         break
113                                 fi
114                         done
115                         # if the file is a generated file then we know this is a font dir (as
116                         # opposed to something like encodings or util) and a candidate for
117                         # removal.  if it's not generated then it's an "otherfile".
118                         ${generated} && candidate=true || otherfile=true
119                         # if the directory is both a candidate for removal and contains at
120                         # least one "otherfile" then don't remove it.
121                         [[ ${candidate} == ${otherfile} ]] && break
122                 done
123                 # if in the end we only have generated files, purge the directory.
124                 if [[ ${candidate} == true && ${otherfile} == false ]]; then
125                         # we don't want to remove fonts.alias files that were installed by
126                         # media-fonts/font-alias. any other fonts.alias files will have
127                         # already been unmerged with their packages.
128                         for g in ${genfiles}; do
129                                 [[ ${g} != fonts.alias && ( -e ${d}/${g} || -L ${d}/${g} ) ]] \
130                                         && rm "${d}"/${g}
131                         done
132                         # if there's nothing left remove the directory
133                         find "${d}" -maxdepth 0 -type d -empty -exec rmdir '{}' \;
134                 fi
135         done
136         eend 0
137 }
138
139 # @FUNCTION: font_pkg_setup
140 # @DESCRIPTION:
141 # The font pkg_setup function.
142 # Collision protection and Prefix compat for eapi < 3.
143 font_pkg_setup() {
144         # Prefix compat
145         case ${EAPI:-0} in
146                 0|1|2)
147                         if ! use prefix; then
148                                 EPREFIX=
149                                 ED=${D}
150                                 EROOT=${ROOT}
151                                 [[ ${EROOT} = */ ]] || EROOT+="/"
152                         fi
153                         ;;
154         esac
155
156         # make sure we get no collisions
157         # setup is not the nicest place, but preinst doesn't cut it
158         [[ -e "${EROOT}/${FONTDIR}/fonts.cache-1" ]] && rm -f "${EROOT}/${FONTDIR}/fonts.cache-1"
159 }
160
161 # @FUNCTION: font_src_install
162 # @DESCRIPTION:
163 # The font src_install function.
164 font_src_install() {
165         local dir suffix commondoc
166
167         set -- ${FONT_S:-${S}}
168         if [[ $# -gt 1 ]]; then
169                 # if we have multiple FONT_S elements then we want to recreate the dir
170                 # structure
171                 for dir in ${FONT_S}; do
172                         pushd "${dir}" > /dev/null
173                         insinto "${FONTDIR}/${dir//${S}/}"
174                         for suffix in ${FONT_SUFFIX}; do
175                                 doins *.${suffix}
176                         done
177                         font_xfont_config "${dir}"
178                         popd > /dev/null
179                 done
180         else
181                 pushd "${FONT_S}" > /dev/null
182                 insinto "${FONTDIR}"
183                 for suffix in ${FONT_SUFFIX}; do
184                         doins *.${suffix}
185                 done
186                 font_xfont_config
187                 popd > /dev/null
188         fi
189
190         font_fontconfig
191
192         [[ -n ${DOCS} ]] && { dodoc ${DOCS} || die "docs installation failed" ; }
193
194         # install common docs
195         for commondoc in COPYRIGHT README{,.txt} NEWS AUTHORS BUGS ChangeLog FONTLOG.txt; do
196                 [[ -s ${commondoc} ]] && dodoc ${commondoc}
197         done
198 }
199
200 # @FUNCTION: font_pkg_postinst
201 # @DESCRIPTION:
202 # The font pkg_postinst function.
203 font_pkg_postinst() {
204         # unreadable font files = fontconfig segfaults
205         find "${EROOT}"usr/share/fonts/ -type f '!' -perm 0644 -print0 \
206                 | xargs -0 chmod -v 0644 2>/dev/null
207
208         if [[ -n ${FONT_CONF[@]} ]]; then
209                 local conffile
210                 echo
211                 elog "The following fontconfig configuration files have been installed:"
212                 elog
213                 for conffile in "${FONT_CONF[@]}"; do
214                         if [[ -e ${EROOT}etc/fonts/conf.avail/$(basename ${conffile}) ]]; then
215                                 elog "  $(basename ${conffile})"
216                         fi
217                 done
218                 elog
219                 elog "Use \`eselect fontconfig\` to enable/disable them."
220                 echo
221         fi
222
223         if has_version media-libs/fontconfig && [[ ${ROOT} == / ]]; then
224                 ebegin "Updating global fontcache"
225                 fc-cache -fs
226                 eend $?
227         else
228                 einfo "Skipping fontcache update (media-libs/fontconfig is not installed or ROOT != /)"
229         fi
230 }
231
232 # @FUNCTION: font_pkg_postrm
233 # @DESCRIPTION:
234 # The font pkg_postrm function.
235 font_pkg_postrm() {
236         font_cleanup_dirs
237
238         # unreadable font files = fontconfig segfaults
239         find "${EROOT}"usr/share/fonts/ -type f '!' -perm 0644 -print0 \
240                 | xargs -0 chmod -v 0644 2>/dev/null
241
242         if has_version media-libs/fontconfig && [[ ${ROOT} == / ]]; then
243                 ebegin "Updating global fontcache"
244                 fc-cache -fs
245                 eend $?
246         else
247                 einfo "Skipping fontcache update (media-libs/fontconfig is not installed or ROOT != /)"
248         fi
249 }