Search code examples
cluavips

How to reduce space between lines in text using VIPS?


Using VIPS, how to reduce the space between lines in a text? There is an optional parameter for vips.Image.text() called spacing that determines this space between lines. However, I can make spacing larger but not smaller. E.g., in the code below (using the Lua binding for VIPS, lua-vips), passing 0 as the argument for spacing...

local vips = require 'vips'
local t = vips.Image.text("This is a\nrandom test.", {
  spacing = 0
})
t:write_to_file("test.png")

produces the following output:

Test with 0 spacing

As expected, using greater values makes spacing larger, so maybe using negative values would make spacing smaller. However, what happens is that the lib gives me a warning:

(lua-vips:17404): GLib-GObject-WARNING **: value "-1" of type 'gint' is invalid or out of range for property 'spacing' of type 'gint'

I know VIPS uses Pango to work with text, so I'm not sure if perhaps this is something that VIPS does not yet support, or something that Pango does not yet support.


Solution

  • It looks like pango does not support spacing < 0:

    https://developer.gnome.org/pango/stable/pango-Layout-Objects.html#pango-layout-set-line-spacing

    https://developer.gnome.org/pango/stable/pango-Layout-Objects.html#pango-layout-set-spacing

    So I think you might be out of luck. You could render lines separately and then position them yourself, I suppose.