font.eclass 6.6 KB

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