diff options
Diffstat (limited to 'nixpkgs/pkgs/applications/misc/k2pdfopt/default.nix')
-rw-r--r-- | nixpkgs/pkgs/applications/misc/k2pdfopt/default.nix | 150 |
1 files changed, 120 insertions, 30 deletions
diff --git a/nixpkgs/pkgs/applications/misc/k2pdfopt/default.nix b/nixpkgs/pkgs/applications/misc/k2pdfopt/default.nix index 8899654cc4c..75e467d4cdf 100644 --- a/nixpkgs/pkgs/applications/misc/k2pdfopt/default.nix +++ b/nixpkgs/pkgs/applications/misc/k2pdfopt/default.nix @@ -1,9 +1,8 @@ -{ stdenv, fetchzip, fetchurl, fetchpatch, cmake, pkgconfig -, zlib, libpng +{ stdenv, runCommand, fetchzip, fetchurl, fetchpatch, fetchFromGitHub +, cmake, pkgconfig, zlib, libpng, makeWrapper , enableGSL ? true, gsl , enableGhostScript ? true, ghostscript , enableMuPDF ? true, mupdf -, enableJPEG2K ? false, jasper ? null # disabled by default, jasper has unfixed CVE , enableDJVU ? true, djvulibre , enableGOCR ? false, gocr # Disabled by default due to crashes , enableTesseract ? true, leptonica, tesseract4 @@ -11,44 +10,132 @@ with stdenv.lib; -stdenv.mkDerivation rec { - pname = "k2pdfopt"; - version = "2.51a"; +# k2pdfopt is a pain to package. It requires modified versions of mupdf, +# leptonica, and tesseract. Instead of shipping patches for these upstream +# packages, k2pdfopt includes just the modified source files for these +# packages. The individual files from the {mupdf,leptonica,tesseract}_mod/ +# directories are intended to replace the corresponding source files in the +# upstream packages, for a particular version of that upstream package. +# +# There are a few ways we could approach packaging these modified versions of +# mupdf, leptonica, and mupdf: +# 1) Override the upstream source with a new derivation that involves copying +# the modified source files from k2pdfopt and replacing the corresponding +# source files in the upstream packages. Since the files are intended for a +# particular version of the upstream package, this would not allow us to easily +# use updates to those packages in nixpkgs. +# 2) Manually produce patches which can be applied against the upstream +# project, and have the same effect as replacing those files. This is what I +# believe k2pdfopt should do this for us anyway. The benefit of creating and +# applying patches in this way is that minor updates (esp. security fixes) to +# upstream packages might still allow these patches to apply successfully. +# 3) Automatically produce these patches inside a nix derivation. This is the +# approach taken here, using the "mkPatch" provided below. This has the +# benefit of easier review and should hopefully be simpler to update in the +# future. + +let + # Create a patch against src based on changes applied in patchCommands + mkPatch = { name, src, patchCommands }: runCommand "${name}-k2pdfopt.patch" { inherit src; } '' + source $stdenv/setup + unpackPhase - src = (fetchzip { - url = "http://www.willus.com/k2pdfopt/src/k2pdfopt_v2.51_src.zip"; - sha256 = "133l7xkvi67s6sfk8cfh7rmavbsf7ib5fyksk1ci6b6sch3z2sw9"; - }); + orig=$sourceRoot + new=$sourceRoot-modded + cp -r $orig/. $new/ - # Note: the v2.51a zip contains only files to be replaced in the v2.50 zip. - v251a_src = (fetchzip { - url = "http://www.willus.com/k2pdfopt/src/k2pdfopt_v2.51a_src.zip"; - sha256 = "0vvwblii7kgdwfxw8dzk6jbmz4dv94d7rkv18i60y8wkayj6yhl6"; - }); + pushd $new >/dev/null + ${patchCommands} + popd >/dev/null - postUnpack = '' - cp -r ${v251a_src}/* $sourceRoot + diff -Naur $orig $new > $out || true ''; - patches = [ ./k2pdfopt.patch ./k2pdfopt-mupdf-1.16.1.patch ]; + pname = "k2pdfopt"; + version = "2.53"; + k2pdfopt_src = fetchzip { + url = "http://www.willus.com/${pname}/src/${pname}_v${version}_src.zip"; + sha256 = "1fna8bg3pascjfc3hmc6xn0xi2yh7f1qp0d344mw9hqanbnykyy8"; + }; +in stdenv.mkDerivation rec { + inherit pname version; + src = k2pdfopt_src; + + patches = [ + ./0001-Fix-CMakeLists.patch + ]; + + postPatch = '' + substituteInPlace willuslib/bmpdjvu.c \ + --replace "<djvu.h>" "<libdjvu/ddjvuapi.h>" + ''; - nativeBuildInputs = [ cmake pkgconfig ]; + nativeBuildInputs = [ cmake pkgconfig makeWrapper ]; buildInputs = let - # The patches below were constructed by taking the files from k2pdfopt in - # the {mupdf,leptonica,tesseract}_mod/ directories, replacing the - # corresponding files in the respective source trees, resolving any errors - # with more recent versions of these depencencies, and running diff. - mupdf_modded = mupdf.overrideAttrs (attrs: { - patches = attrs.patches ++ [ ./mupdf.patch ]; # Last verified with mupdf 1.16.1 + # We use specific versions of these sources below to match the versions + # used in the k2pdfopt source. Note that this does _not_ need to match the + # version used elsewhere in nixpkgs, since it is only used to create the + # patch that can then be applied to the version in nixpkgs. + mupdf_patch = mkPatch { + name = "mupdf"; + src = fetchurl { + url = "https://mupdf.com/downloads/archive/mupdf-1.17.0-source.tar.gz"; + sha256 = "13nl9nrcx2awz9l83mlv2psi1lmn3hdnfwxvwgwiwbxlkjl3zqq0"; + }; + patchCommands = '' + cp ${k2pdfopt_src}/mupdf_mod/{filter-basic,font,stext-device,string}.c ./source/fitz/ + cp ${k2pdfopt_src}/mupdf_mod/pdf-* ./source/pdf/ + ''; + }; + mupdf_modded = mupdf.overrideAttrs ({ patches ? [], ... }: { + patches = patches ++ [ mupdf_patch ]; + # This function is missing in font.c, see font-win32.c + postPatch = '' + echo "void pdf_install_load_system_font_funcs(fz_context *ctx) {}" >> source/fitz/font.c + ''; }); - leptonica_modded = leptonica.overrideAttrs (attrs: { - patches = [ ./leptonica.patch ]; # Last verified with leptonica 1.78.0 + + leptonica_patch = mkPatch { + name = "leptonica"; + src = fetchurl { + url = "http://www.leptonica.org/source/leptonica-1.79.0.tar.gz"; + sha256 = "1n004gv1dj3pq1fcnfdclvvx5nang80336aa67nvs3nnqp4ncn84"; + }; + patchCommands = "cp -r ${k2pdfopt_src}/leptonica_mod/. ./src/"; + }; + leptonica_modded = leptonica.overrideAttrs ({ patches ? [], ... }: { + patches = patches ++ [ leptonica_patch ]; }); + + tesseract_patch = mkPatch { + name = "tesseract"; + src = fetchFromGitHub { + owner = "tesseract-ocr"; + repo = "tesseract"; + rev = "4.1.1"; + sha256 = "1ca27zbjpx35nxh9fha410z3jskwyj06i5hqiqdc08s2d7kdivwn"; + }; + patchCommands = '' + cp ${k2pdfopt_src}/tesseract_mod/{baseapi,tesscapi,tesseract}.* src/api/ + cp ${k2pdfopt_src}/tesseract_mod/{tesscapi,tessedit,tesseract}.* src/ccmain/ + cp ${k2pdfopt_src}/tesseract_mod/dotproduct{avx,fma,sse}.* src/arch/ + cp ${k2pdfopt_src}/tesseract_mod/{intsimdmatrixsse,simddetect}.* src/arch/ + cp ${k2pdfopt_src}/tesseract_mod/{errcode,genericvector,mainblk,params,serialis,tessdatamanager,tess_version,tprintf,unicharset}.* src/ccutil/ + cp ${k2pdfopt_src}/tesseract_mod/{input,lstmrecognizer}.* src/lstm/ + cp ${k2pdfopt_src}/tesseract_mod/openclwrapper.* src/opencl/ + ''; + }; tesseract_modded = tesseract4.override { - tesseractBase = tesseract4.tesseractBase.overrideAttrs (_: { - patches = [ ./tesseract.patch ]; # Last verified with tesseract 1.4 + tesseractBase = tesseract4.tesseractBase.overrideAttrs ({ patches ? [], ... }: { + patches = patches ++ [ tesseract_patch ]; + # Additional compilation fixes + postPatch = '' + echo libtesseract_api_la_SOURCES += tesscapi.cpp >> src/api/Makefile.am + substituteInPlace src/api/tesseract.h \ + --replace "#include <leptonica.h>" "//#include <leptonica.h>" + ''; }); }; in @@ -56,7 +143,6 @@ stdenv.mkDerivation rec { optional enableGSL gsl ++ optional enableGhostScript ghostscript ++ optional enableMuPDF mupdf_modded ++ - optional enableJPEG2K jasper ++ optional enableDJVU djvulibre ++ optional enableGOCR gocr ++ optionals enableTesseract [ leptonica_modded tesseract_modded ]; @@ -71,6 +157,10 @@ stdenv.mkDerivation rec { install -D -m 755 k2pdfopt $out/bin/k2pdfopt ''; + preFixup = optionalString enableTesseract '' + wrapProgram $out/bin/k2pdfopt --set-default TESSDATA_PREFIX ${tesseract4}/share/tessdata + ''; + meta = with stdenv.lib; { description = "Optimizes PDF/DJVU files for mobile e-readers (e.g. the Kindle) and smartphones"; homepage = "http://www.willus.com/k2pdfopt"; |