# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit meson

DESCRIPTION="Paged HTML Rendering Library"
HOMEPAGE="https://github.com/plutoprint/plutobook"
SRC_URI="https://github.com/plutoprint/plutobook/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64"

# PlutoBook is kinda useless without support for webp and turbojpeg.
IUSE="+autodetect-ca curl examples test +tools +jpeg +webp"

DEPEND="
    x11-libs/cairo
    media-libs/freetype
    media-libs/harfbuzz
    media-libs/fontconfig
    dev-libs/expat
    dev-libs/icu
    curl? ( net-misc/curl )
    jpeg? ( media-libs/libjpeg-turbo )
    webp? ( media-libs/libwebp )"
RDEPEND="${DEPEND}"
BDEPEND=""

PATCHES=(
    "${FILESDIR}/${P}-page_margin.patch"
    "${FILESDIR}/${P}-positioning.patch"
)

src_prepare() {
    # use non-generic names to avoid interferrence with other packages
    sed -i -e 's/html2pdf/pb-html2pdf/g' -e 's/html2png/pb-html2png/g' \
        CHANGELOG.md || die
    sed -i -e "s/html2pdf'/pb-html2pdf'/g" -e "s/html2png'/pb-html2png'/g" \
        tools/meson.build || die	
    sed -i 's/html2pdf"/pb-html2pdf"/g' tools/html2pdf.cpp || die
    sed -i 's/html2png"/pb-html2png"/g' tools/html2png.cpp || die

    default
}

src_configure() {
    local emesonargs=(
        $(meson_feature autodetect-ca)
        $(meson_feature curl)
        $(meson_feature examples)
        $(meson_feature test tests)
        $(meson_feature tools)
        $(meson_feature jpeg turbojpeg)
        $(meson_feature webp)
    );

    meson_src_configure
}