7

This relates to the "gm" extension for node, http://aheckmann.github.io/gm/docs.html

I need to add some text centered around a bounding box (horizontally is enough). The function drawText() requires x,y coordinates, but there is no way to draw centered text.

I would otherwise need a function which can return the width of a text string in the font/size given, so I can calculate my starting x position in javascript, before calling drawText().

Rildo Pragana
  • 83
  • 1
  • 4

1 Answers1

9

You can use the region and gravity functions this way:

gm(filePath)
    .region(WIDTH, HEIGHT, X, Y)
    .gravity('Center')
    .fill(color)
    .fontSize(textFontSize)
    .font(font)
    .drawText(0, 0, 'This text will be centered inside the region')
tomericco
  • 1,544
  • 3
  • 19
  • 30
  • Where do X and Y come from? – Victorio Berra Jan 25 '18 at 17:20
  • From your code. These describe the position and size of the bounding box of the text, which will be rendered in the center of it. – tomericco Jan 25 '18 at 17:38
  • 3
    I couldn't get this to work for me at all. I was able to get the region working with a sepia filter, e.g. .region(WIDTH, HEIGHT, X, Y).sepia(), but not with text. Instead I simply did gm(filePath).drawText(0, 250, 'my text', 'North'), to get it centrally aligned. – antun Nov 15 '18 at 16:54