GraphicsMagick and FontMetrics

251 views Asked by At

My aim is to create a text image and put it on top of other images. This is the command I use to create the text image:

    String[] cp_img_arguments = {"convert",
            "-size", width_size + "x",
            "-background", background_colour,
            "-pointsize", pointsize.toString(),
            "-font", font_path,
            "-fill",text_colour,
            "caption:" + text,
            destination};

In order to calculate the size of the image I need, I make use of FontsMetrics like this:

    Font font = new Font(fontName, Font.PLAIN, pointsize);
    Canvas canvas = new Canvas();
    fontMetrics = canvas.getFontMetrics(font);
    width_size = fontMetrics.stringWidth(toString());

Although I often generate images with the text truncated or multi-line, which means the width I calculate is not actually enough for the text. The font comes from a .ttf file. I believe this miss-calculation happens because of the density/DPI parameter in GraphicsMagick. I am not currently using it (default is 55 I believe), but I wonder how that affects the actual font size, compared to what FontMetrics calculate.

Does anyone have experience generating images with text using gm? Any ideas how I can make the image dimension calculation more accurate?

0

There are 0 answers