# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=cairo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_commit='156cd3eaaebfd8635517c2baf61fcf3627ff7ec2'
pkgver=1.17.4
pkgrel=3
pkgdesc="Cairo vector graphics library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://cairographics.org/"
license=(LGPL2.1 MPL1.1)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "git"
            )
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
optdepends=("${MINGW_PACKAGE_PREFIX}-glib2: libcairo-gobject")
options=('strip' 'staticlibs')
source=("https://gitlab.freedesktop.org/cairo/cairo/-/archive/${_commit}/cairo-${_commit}.tar.gz"
        0001-fix-font-names-in-pdf-output.patch
        0026-create-argb-fonts.all.patch
        0027-win32-print-fix-unbounded-surface-assertion.patch
        0030-ucrt-clang-fixes.patch)
sha256sums=('204215866aa392c27d3e1306f6ea1946a623bf86fff3143f777d1e8bf4383bb0'
            'a5926f8bca3cb09e41ed8d2a60fb053c4243fdeec2c36c5c7e15d2a784b6ee9a'
            '6db6c44fbdb4926d09afa978fe80430186c4b7b7d255059602b1f94c6a079975'
            '7e244c20eec8c7b287dbee1d34de178d9b0c419dc4c2b11c90eaf626c92bf781'
            '86c1af2878a20bd3608fc476e4ba1f2451458a5f6f12e457e37b61082c38db82')

prepare() {
  mv "${srcdir}/${_realname}-${_commit}" "${srcdir}/${_realname}-${pkgver}"
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/125
  patch -p1 -i ${srcdir}/0001-fix-font-names-in-pdf-output.patch

  patch -p1 -i ${srcdir}/0026-create-argb-fonts.all.patch

  # https://gitlab.freedesktop.org/cairo/cairo/-/issues/131
  patch -p1 -i ${srcdir}/0027-win32-print-fix-unbounded-surface-assertion.patch

  patch -p1 -i ${srcdir}/0030-ucrt-clang-fixes.patch
}

build() {
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix=${MINGW_PREFIX} \
    --buildtype=plain \
    --wrap-mode=nofallback \
    --default-library=both \
    -Dauto_features=enabled \
    -Dxlib=disabled \
    -Dxcb=disabled \
    -Dgl-backend=disabled \
    -Dtests=disabled \
    -Dspectre=disabled \
    -Dtee=enabled \
    ../${_realname}-${pkgver}

  meson compile
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" meson install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING"          "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING-LGPL-2.1" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-LGPL-2.1"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING-MPL-1.1"  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-MPL-1.1"
}
