################################################################################
#
# font.cygclass - functions for installing X11 fonts
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/font.cygclass
#  DESCRIPTION
#  This cygclass provides functions for installing fonts so that they
#  will be found by the X server (for Xaw/Motif applications) and fontconfig
#  (used by most modern GUI toolkits).
#  EXAMPLE
#    ORIG_PN="cantarell-fonts"
#    inherit gnome.org font
#    
#    NAME="font-cantarell-otf"
#    VERSION=0.0.12
#    RELEASE=1
#    CATEGORY="X11"
#    SUMMARY="Cantarell TTF sans serif fonts"
#    DESCRIPTION="The Cantarell typeface family was designed as a contemporary
#    Humanist sans serif, and was developed for on-screen reading; in particular,
#    reading web pages on a mobile phone."
#    HOMEPAGE="http://abattis.org/cantarell/"
#    
#    src_compile() { :; }
#    
#    src_install() {
#      cd ${S}
#      fontinto OTF
#      dofont otf/*.otf
#      
#      dofontconf fontconfig/*.conf
#    }
#  NOTE
#  Regardless of the usage of font.cygclass, cygport will compress all
#  bitmap fonts (.pcf) and create postinstall scripts for all fonts
#  installed in FONTSDIR in order to register them with the X server and
#  fontconfig.
#****

#****d* font.cygclass/FONTSDIR
#  DESCRIPTION
#  The top font directory, AKA the fontrootdir.
#****
FONTSDIR="/usr/share/fonts"


#****C* font.cygclass/sfd2ttf
#  SYNOPSIS
#  sfd2ttf SFD_FILE[S] ...
#  DESCRIPTION
#  Creates TrueType Font (TTF) files from FontForge Spline Font Database
#  (SFD) sources.
#****
sfd2ttf() {
	local f

	check_prog_req fontforge

	for f in $@
	do
		[ -e $f ] || error "sfd2ttf: $f: File not found"
		echo -n "Converting ${f##*/} to TTF... "
		fontforge -lang=ff -c 'Open($1);Generate($fontname+".ttf","ttf")' $f &> /dev/null || error "fontforge: $f: Generate failed"
		echo "Done."
	done
}

#****I* font.cygclass/fontinto
#  SYNOPSIS
#  fontinto SUBDIR
#  DESCRIPTION
#  Tells dofont which font directory to install into.  Accepts a single
#  argument, the name of the font subdirectory.
#  NOTE
#  Technically any subdir can be used for fonts, and fontconfig will
#  find the font no matter which subdir you use.  OTOH, the X server by
#  default searches only in a specific list of subdirectories, namely:
#  * misc:  miscellaneous bitmap fonts (.pcf.gz)
#  * 75dpi:  lower-resolution standard bitmap fonts (.pcf.gz)
#  * 100dpi:  higher-resolution standard bitmap fonts (.pcf.gz)
#  * OTF:  OpenType fonts (.otf)
#  * TTF:  TrueType fonts (.ttf)
#  * Type1:  Postscript fonts (.afm, .pfa/.pfb)
#  Fonts installed into any other subdirectory will not be found by the
#  X server without the user adjusting their fontpath accordingly.
#****
fontinto() {
	if (( $# != 1 ))
	then
		error "fontinto accepts exactly one argument"
	fi

	case ${1} in
		/*)
			error "argument should be only a subdirectory, not a full path"
			;;
	esac

	dodir ${FONTSDIR}/${1}
	_fontinto_dir=${FONTSDIR}/${1}
}

#****I* font.cygclass/dofont
#  SYNOPSIS
#  dofont FONTFILE [FONTFILE] ...
#  DESCRIPTION
#  Installs the given font file(s) into the font subdirectory specified by
#  the most recent call to fontinto.
#****
dofont() {
	if ! defined _fontinto_dir || [ ! -d ${D}${_fontinto_dir} ]
	then
		error "fontinto must be called before dofont"
	fi

	for i
	do
		if [ ! -e ${i} ]
		then
			error "file ${i} does not exist"
		fi

		__doinstall 0644 ${i} ${_fontinto_dir} || error "dofont ${i} failed"
	done
}

#****I* font.cygclass/dofontconf
#  SYNOPSIS
#  dofontconf CONFFILE [CONFFILE] ...
#  DESCRIPTION
#  Installs the given font configuration file(s) into the system fontconfig
#  configuration directories.  If file is not named [0-9][0-9]*.conf, then
#  use newfontconf instead to rename it.
#****
dofontconf() {
	for i
	do
		if [ ! -e ${i} ]
		then
			error "dofontconf: ${i}: does not exist"
		fi

		case ${i##*/} in
		[0-9][0-9]*.conf) ;;
		*) error "dofontconf: ${i}: must be named [0-9][0-9]*.conf" ;;
		esac

		dodir /usr/share/fontconfig/conf.avail /etc/fonts/conf.d
		__doinstall 0644 ${i} /usr/share/fontconfig/conf.avail || error "dofontconf ${i} failed"
		dosym /usr/share/fontconfig/conf.avail/${i##*/} /etc/fonts/conf.d/${i##*/}
	done
}

#****I* font.cygclass/newfontconf
#  SYNOPSIS
#  newfontconf CONFFILE DESIRED_NAME
#  DESCRIPTION
#  Installs the given font configuration file into the system fontconfig
#  configuration directories as the given DESIRED_NAME, which must be of
#  the form [0-9][0-9]*.conf:
#  * 00 through 09: Font directories
#  * 10 through 19: system rendering defaults (AA, etc)
#  * 20 through 29: font rendering options
#  * 30 through 39: family substitution
#  * 40 through 49: generic identification, map family->generic
#  * 50 through 59: alternate config file loading
#  * 60 through 69: generic aliases, map generic->family
#  * 70 through 79: select font (adjust which fonts are available)
#  * 80 through 89: match target="scan" (modify scanned patterns)
#  * 90 through 99: font synthesis
#****
newfontconf() {
	if [ ! -e ${1} ]
	then
		error "newfontconf: ${1}: does not exist"
	fi

	case ${2} in
	[0-9][0-9]*.conf) ;;
	*) error "newfontconf: ${2}: must be named [0-9][0-9]*.conf" ;;
	esac

	dodir /usr/share/fontconfig/conf.avail /etc/fonts/conf.d
	__doinstall 0644 ${1} /usr/share/fontconfig/conf.avail ${2} || error "newfontconf ${1} failed"
	dosym /usr/share/fontconfig/conf.avail/${2} /etc/fonts/conf.d/${2}
}

readonly -f sfd2ttf fontinto dofont dofontconf newfontconf
