Search code examples
imagemagickgraphicsmagick

Get width of drawText in GraphicsMagick


I have a variable width word that I would like to center in a fixed sized box. How can I calculate the width of the text so that I know how far the left offset should be?


Solution

  • You did not specify your programming language so I am assuming you are using C.

    You can determine the width with the MagickQueryFontMetrics methods (http://www.graphicsmagick.org/wand/magick_wand.html#magickqueryfontmetrics) this returns a double array containing the following information:

    • 0 character width
    • 1 character height
    • 2 ascender
    • 3 descender
    • 4 text width
    • 5 text height
    • 6 maximum horizontal advance

    Or if you could use use the GetTypeMetrics method that gives you the information in a nice struct:

    • GetTypeMetrics(Image *image,const DrawInfo *draw_info, TypeMetric *metrics)