1

When I add text annotations to an image without showing the image, the font family is always monospace. How do I fix this?

Edit: After some more testing the problem appears for both, with showImage() and without. When opening the saved image the font family is lost.


I want to open all image files in one directory and add annotations to every file without showing the file to the user. Everything is working fine except that the font family always is monospace.

The following image shows the text added without a call of showImage() on the left. On the right this is the exact same code but with one call of showImage(). As one can see, the font family is wrong. The code is shown below.

Edit: As mentioned above, the right image is equal to the left image when I close the right image and open it again.

Text annotations created with showImage() and without showImage()

String path = "/temporary/path/for/saving/test-img.dm4";

// create dummy image and save it
image img := RealImage("The Image", 4, 512, 512);
img = itheta
img.saveImage(path);

// - - -
// open the image and process it

img := openImage(path);
// img.showImage(); // <- This makes the difference between left and right image 
                    //    *Edit*: Both images loose their font family if they are
                    //    closed and opened again

ImageDisplay disp;
if(img.ImageCountImageDisplays() == 0){
    disp = ImageCreateImageDisplay(img, -2);
}
else{
    disp = img.ImageGetImageDisplay(0);
}

Component text = NewTextAnnotation(100, 300, "Some text", 20);
text.ComponentSetForegroundColor(0, 0, 140);
text.ComponentSetFontFaceName("sans-serif");
disp.ComponentAddChildAtEnd(text);

// save so the image can be opened if it is not shown anyway
img.saveImage(path);
miile7
  • 2,547
  • 3
  • 23
  • 38
  • 1
    I ran your script in both **GMS 3.4.0.2804** and **GMS 3.4.1.2938** and could not reproduce the problem. However, I don't have "sans-serif" on my system, so I tried another of the available fonts (from the list when right-clicking an annotation). If you set a non-existing font, then the system usually uses an approximated font - and maybe the approximation code is different when running the script or opening a file? (The actual relative DPI size can play a role here.) Could this be your issue? Or have you verified that "sans-serif" is the font after the script by right-clicking? – BmyGuest Apr 30 '20 at 17:47
  • No, I haven't verified that. That may be the issue. I come from web development and "sans-serif" or "serif" is the most general font you can set so I thought I'm on the safe side this way. But I always and only tried "sans-serif". I will check that. – miile7 May 02 '20 at 14:19

1 Answers1

1

All Credit goes to @BmyGuest. As he suggested in his comment one should only use the fonts that are installed. Fonts like "sans-serif" do not exist. To find out which fonts are installed, use (as @BmyGuest said) the menu when right-clicking an annotation.

If you want to get the fonts installed directly you can use this answer (from @BmyGuest of course :)) which selects all the available fonts.

miile7
  • 2,547
  • 3
  • 23
  • 38