nixpkgs icon indicating copy to clipboard operation
nixpkgs copied to clipboard

ImageMagick: Enable Pango when enabling librsvg

Open YoshiRulz opened this issue 1 year ago • 2 comments

Description of changes

Added pango to rundeps (Pango would be part of IM's closure anyway as it's required by librsvg) and passed --with-pango. This enables the niche rich text markup feature.

I went to check if ImageMagick was working with this changeset, and it seemed to be, apart from the Pango integration :( It prints

Fontconfig error: Cannot load default config file: No such file: (null)
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories

though that may be because I'm on non-NixOS and don't have a Nix fontconfig installed. (Comparing this against the baseline of running the same magick invocation with IM installed from the Arch repos, which I got working earlier.) I've marked this PR as a draft until that's been debugged. edit: I've done the same as https://github.com/NixOS/nixpkgs/commit/f9943cd28a2520831f27aceadec598ef04b8019c / https://github.com/NixOS/nixpkgs/pull/172097/commits/8bb5266e2ad109a1f1087b0a45fc10ce36ed8ddd but it hasn't fixed the errors.

Things done
  • Built on platform(s)
    • [x] x86_64-linux
    • [ ] aarch64-linux
    • [ ] x86_64-darwin
    • [ ] aarch64-darwin
  • [ ] For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • [ ] Tested, as applicable:
  • [ ] Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • [x] Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.11 Release Notes (or backporting 22.05 Release notes)
    • [ ] (Package updates) Added a release notes entry if the change is major or breaking
    • [ ] (Module updates) Added a release notes entry if the change is significant
    • [ ] (Module addition) Added a release notes entry if adding a new NixOS module
    • [ ] (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • [x] Fits CONTRIBUTING.md.

YoshiRulz avatar Sep 22 '22 01:09 YoshiRulz

https://github.com/NixOS/nixpkgs/pull/192344/commits/b330050d7faff69cd8296ac702ce90166d50b6b3 doesn't make Pango work, but it does make simple label: text work (though obviously only DejaVu Sans from freefont_ttf is available). But even without that fix, you can still use absolute paths to other fonts as suggested in https://github.com/NixOS/nixpkgs/issues/153884#issuecomment-1065949506. Should I open a separate PR?

YoshiRulz avatar Sep 25 '22 22:09 YoshiRulz

Could you post a command for testing? I could try wether this works on NixOS.

Unfortunately I don't have too much of a clue of Imagemagick and fontconfig (despite being one of the maintainer).

I think as long as nothing breaks, it could be worthwile to merge incomplete solutions. But I think I have to understand the feature better you're trying to make possible here.

erictapen avatar Sep 26 '22 09:09 erictapen

repro.sh:

#!/bin/sh
echo '====='
magick --version
echo '===== simple text'
magick -background '#212121' -fill white -font DejaVu-Sans -size 200x50 -gravity center label:'Hello, world!' simple_text.png
magick identify simple_text.png
echo '===== Pango text'
ls -l script.pango.txt
magick -background '#212121' -size 150x80 -gravity center pango:@script.pango.txt pango_text.png
magick identify pango_text.png
echo '====='

script.pango.txt:

<markup><span fgcolor="white" font_family="DejaVu Sans">multi-line with
<span font_size="150%">LARGE</span>
text in middle</span></markup>

default.nix:

{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz") {} }:
pkgs.stdenv.mkDerivation {
	pname = "repro";
	version = "0.1.0";
	src = builtins.path { path = ./.; name = "source"; };
	nativeBuildInputs = [
		(pkgs.imagemagick.overrideAttrs (oldAttrs: {
			# sufficient to fix Pango text
#			buildInputs = oldAttrs.buildInputs ++ [ pkgs.pango ];
#			configureFlags = oldAttrs.configureFlags ++ [ "--with-pango" ];
			# these were reported to fix the fontconfig error messages for other packages, but don't seem to work for IM
#			FONTCONFIG_FILE = pkgs.makeFontsConf { fontDirectories = [ pkgs.freefont_ttf ]; }; # Fontconfig error: Cannot load default config file
#			preBuild = ''export XDG_CACHE_HOME="$(mktemp -d)"''; # Resolves the warning "Fontconfig error: No writable cache directories"
		}))
	];
	dontPatch = true;
	dontConfigure = true;
	buildPhase = ''rm -f *.png && ./repro.sh'';
	installPhase = ''mkdir -p $out && mv *.png $out'';
	dontFixup = true;
}

output of ./repro.sh (Manjaro):

=====
Version: ImageMagick 7.1.0-27 Q16-HDRI x86_64 2022-03-04 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP(4.5) 
Delegates (built-in): bzlib cairo djvu fontconfig freetype heic jbig jng jp2 jpeg jxl lcms lqr ltdl lzma openexr pangocairo png raqm raw rsvg tiff webp wmf x xml zip zlib
Compiler: gcc (11.2)
===== simple text
simple_text.png PNG 200x50 200x50+0+0 16-bit Grayscale Gray 2765B 0.000u 0:00.000
===== Pango text
-rw-r--r-- 1 yoshi yoshi 139 Sep 27 01:52 script.pango.txt
pango_text.png PNG 150x80 150x80+0+0 16-bit sRGB 7661B 0.000u 0:00.000
=====

and the two images look like this: simple_text.png pango_text.png output of nix-build --pure:

[...]
=====
Version: ImageMagick 7.1.0-48 Q16-HDRI x86_64 c1ca24765:20220910 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5) 
Delegates (built-in): bzlib cairo djvu fontconfig freetype heic jng jp2 jpeg jxl lcms lqr lzma openexr png raw rsvg tiff webp x xml zlib
Compiler: gcc (11.3)
===== simple text
Fontconfig error: Cannot load default config file: No such file: (null)
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
simple_text.png PNG 200x50 200x50+0+0 16-bit Grayscale Gray 2287B 0.000u 0:00.000
===== Pango text
-rw-r--r-- 1 nixbld nixbld 139 Jan  1  1970 script.pango.txt
magick: unable to open image '@script.pango.txt': No such file or directory @ error/blob.c/OpenBlob/3568.
magick: no decode delegate for this image format `PANGO' @ error/constitute.c/ReadImage/741.
identify: unable to open image 'pango_text.png': No such file or directory @ error/blob.c/OpenBlob/3568.
=====
[...]

As you can see, the current version of IM is able to generate simple_text.png correctly, but not pango_text.png.

But the --with-pango fix does enable Pango support! I'm not sure what I was doing before, whether I had a rogue copy in my environment, or maybe I'd just assumed that error message == failure, as the same fontconfig errors appear during the Pango text generation with the fix applied.

[...]
===== Pango text
-rw-r--r-- 1 nixbld nixbld 139 Jan  1  1970 script.pango.txt
Fontconfig error: Cannot load default config file: No such file: (null)
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
Fontconfig error: No writable cache directories
pango_text.png PNG 150x80 150x80+0+0 16-bit Grayscale Gray 2754B 0.000u 0:00.000
=====
[...]

So in summary, my first commit enables Pango support, albeit with only DejaVu Sans (<span font_family/> doesn't seem to accept absolute paths), and my second commit is useless. If I install the patched imagemagick in my environment and run it outside the Nix sandbox, I'm able to use my non-Nix system fonts.

YoshiRulz avatar Sep 26 '22 17:09 YoshiRulz

Thank you so much! This is very helpful.

Am I right in that you'd just need to remove the second commit? Besides that it looks mergeable to me.

erictapen avatar Sep 26 '22 20:09 erictapen

Could you just remove the last commit instead of reverting it? E.g. by using git rebase -i HEAD^^^.

We don't want unnecessary commits in the history on master.

erictapen avatar Sep 26 '22 21:09 erictapen