SUMMARY="A free, high-quality, and portable font engine"
DESCRIPTION="FreeType is written in C, designed to be small, efficient, \
highly customizable, and portable while capable of producing high-quality \
output (glyph images) of most vector and bitmap font formats."
HOMEPAGE="https://www.freetype.org/"
COPYRIGHT="1996-2021 David Turner, Robert Wilhelm, Werner Lemberg, et al."
LICENSE="FreeType"
REVISION="1"
SOURCE_URI="https://download.savannah.gnu.org/releases/freetype/freetype-$portVersion.tar.xz"
CHECKSUM_SHA256="3333ae7cfda88429c97a7ae63b7d01ab398076c3b67182e960e5684050f2c5c8"
PATCHES="
	enable-validation-modules.patchset
	python3.patch
	"
#PATCHES="freetype-subpixel.patchset" # enable patented subpixel rendering

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

PROVIDES="
	freetype$secondaryArchSuffix = $portVersion
	lib:libfreetype$secondaryArchSuffix = 6.18.1 compat >= 6
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libbrotlidec$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libz$secondaryArchSuffix >= 1
	"
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	REQUIRES="$REQUIRES
			lib:libharfbuzz$secondaryArchSuffix
		"
fi

PROVIDES_devel="
	freetype${secondaryArchSuffix}_devel = $portVersion
	devel:libfreetype${secondaryArchSuffix} = 6.18.1 compat >= 6
	"
REQUIRES_devel="
	freetype${secondaryArchSuffix} == $portVersion base
	devel:libbrotlidec$secondaryArchSuffix
	devel:libbz2$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	REQUIRES_devel="$REQUIRES_devel
			devel:libharfbuzz$secondaryArchSuffix
		"
fi

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libbrotlidec$secondaryArchSuffix
	devel:libbz2$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	BUILD_REQUIRES="$BUILD_REQUIRES
			devel:libharfbuzz$secondaryArchSuffix
		"
fi
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:meson
	cmd:pkg_config$secondaryArchSuffix
	cmd:python3
	"

defineDebugInfoPackage freetype$secondaryArchSuffix \
	$libDir/libfreetype.so.6.18.1

BUILD()
{
	unset meson
	meson setup build . --buildtype release \
		--prefix $prefix \
		--bindir $binDir \
		--datadir $dataDir \
		--includedir $includeDir \
		--libdir $libDir \
		--default-library shared
	meson compile -C build $jobArgs
}

INSTALL()
{
	unset meson
	meson install -C build

	install -Dt $dataRootDir/aclocal -m644 builds/unix/freetype2.m4

	prepareInstalledDevelLibs libfreetype
	fixPkgconfig

	# The libtool files reference other libraries using the wrong paths, which
	# creates a lot of confusion. Fix them so correct paths are used.
	local develPackageName="${portName}_devel-$portFullVersion"
	local packageLinksDir=$(dirname $portPackageLinksDir)
	local linksDir="$packageLinksDir/${develPackageName}/devel~libpng16$secondaryArchSuffix/$relativeDevelopLibDir"
	sed -i -e "s,^\(Libs.private.*\)-L.* \(-l.*\)$,\1-L$linksDir \2," \
		$developLibDir/pkgconfig/freetype2.pc

	# devel package
	packageEntries devel \
		$dataRootDir/aclocal \
		$developDir
}
