font rendering

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Suntor
Posts: 16
Joined: 2020-03-22T19:04:47-07:00
Authentication code: 1152

font rendering

Post by Suntor »

The left part of the image is captured from the Windows Notepad, the font set as «Microsoft Sans Serif, Regular, 10». The right side of the image is generated by the following ImageMagick command:

Code: Select all

convert
 -size 60x53 xc:white
 -font Microsoft-Sans-Serif -density 96 -pointsize 10 +antialias
 -annotate +2+15 "USA" -annotate +2+31 "Spain" -annotate +2+47 "Germany"
 test.png
Both parts are concatenated and scaled five times. In order to make it easy to compare them.
Image

The results are different. ImageMagick draws text with artifacts. I tried different command options like "-stroke & -strokewidth", but nothing helped. How to remove these artifacts?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: font rendering

Post by fmw42 »

What version of freetype are you using? ImageMagick uses freetype. Perhaps upgrade freetype. Or try a different version of that font.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: font rendering

Post by snibgo »

What version of IM?

With IM v6.9.9-50, I get a different result:
Image
I don't suppose Microsoft uses the same open source font rendering software as ImageMagick, so the results will be different.

Fonts and font software are not designed to work well without antialiasing, so I suggest you remove "+antialias":
Image
snibgo's IM pages: im.snibgo.com

Suntor
Posts: 16
Joined: 2020-03-22T19:04:47-07:00
Authentication code: 1152

Re: font rendering

Post by Suntor »

fmw42 wrote:
2020-03-22T19:57:34-07:00
What version of freetype are you using?

Code: Select all

C:\>convert -list format | find /i "freetype"
    DFONT* TTF       r--   Multi-face font package (Freetype 2.6.3)
      OTF* TTF       r--   Open Type font (Freetype 2.6.3)
      PFA* TTF       r--   Postscript Type 1 font (ASCII) (Freetype 2.6.3)
      PFB* TTF       r--   Postscript Type 1 font (binary) (Freetype 2.6.3)
      TTC* TTF       r--   TrueType font collection (Freetype 2.6.3)
      TTF* TTF       r--   TrueType font (Freetype 2.6.3)
snibgo wrote:
2020-03-22T20:19:29-07:00
What version of IM?

Code: Select all

C:\>convert -version | find /i "ver"
Version: ImageMagick 7.0.5-7 Q16 x64 2017-05-20 http://www.imagemagick.org
snibgo wrote:
2020-03-22T20:19:29-07:00
...so I suggest you remove "+antialias":
This is not an option. The task is to draw the text identically as in normal Windows programs.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: font rendering

Post by fmw42 »

IM 7.0.5.7 is ancient. You might try upgrading. Your freetype is also rather old. Freetype is up to 2.10.1 See https://www.freetype.org

I suggest you try to keep ImageMagick and its delegates updated more frequently. However, I do not know if that will help.

As user snibgo has pointed out, text rendering by Microsoft and by Freetype are going to use different code and so likely will not match perfectly.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: font rendering

Post by snibgo »

Suntor wrote:The task is to draw the text identically as in normal Windows programs.
But normal Windows programs will use anti-aliasing, depending on how the computer is configured. For example, with Notepad on my Windows 8.1 laptop, an enlarged screenshot:
Image
If you want text to look exactly as a particular program would render it, the obvious way is to render the text with that program and grab a screenshot.

Another way would be to write a conventional Windows program that uses Windows system calls to render the text to a memory array. That program could also use ImageMagick calls to write that array to disk as a PNG file or whatever you want.
snibgo's IM pages: im.snibgo.com

Suntor
Posts: 16
Joined: 2020-03-22T19:04:47-07:00
Authentication code: 1152

Re: font rendering

Post by Suntor »

fmw42 wrote:
2020-03-23T18:02:28-07:00
IM 7.0.5.7 is ancient.
I have updated the IM version to the latest ImageMagick 7.0.10-2, containing Freetype 2.10.0. Nothing has changed. Fonts are still drawn with artifacts. The quality is unacceptable. Many characters cannot be recognized.

Code: Select all

C:\>convert -version | find /i "ver"
Version: ImageMagick 7.0.10-2 Q16 x64 2020-03-22 http://www.imagemagick.org
snibgo wrote:
2020-03-23T18:27:12-07:00
But normal Windows programs will use anti-aliasing, depending on how the computer is configured.
I disabled anti-aliasing on my system. As you can see in the example images of text in Notepad. The anti-aliasing is not suitable for my task. I need to draw the text without anti-aliasing, in the specified font, so that I can recognize the characters. This is a simple task. But I can't get results.

I don't need to achieve absolutely identical font drawing as in Windows. I'll make myself clear. I need the fonts were drawn so that it was possible to read them. For example, let's take the «Courier New» font and try to print an ordinary file path:

Code: Select all

C:\>convert
	-font Courier-New -density 96 -pointsize 10 +antialias
	label:"C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows"
	path.png
The result is unacceptable. The backslash (\) characters are not visible at all. They are not drawn:
Image
Notepad displays the same font correctly. All characters are visible:
Image

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: font rendering

Post by magick »

The backslashes are unexpectedly being removed, likely from the string pre-processing. We will have a patch for that in the next few days. We'll also look into improved mono-font rendering.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: font rendering

Post by snibgo »

Cross-posting with magick: I have reported the backslash problem as a bug: https://www.imagemagick.org/discourse-s ... =3&t=37751
snibgo's IM pages: im.snibgo.com

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: font rendering

Post by magick »

Preprocessing removes one of the backslashes so the label becomes: C:\Users\Administrator\AppData\Roaming\Microsoft\Windows. However, backslash is an escape character, see https://imagemagick.org/script/escape.php. This means the label must be label:"C:\\\Users\\\Administrator\\\AppData\\\Roaming\\\Microsoft\\\Windows" to render as expected. Even so, the mono rendering still need a fix to properly render the label. We'll need a few days to identify a proper fix for the rendering problem.

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: font rendering

Post by magick »

Thanks for the problem report. We can reproduce it and will have a patch to fix it in the GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow.

Suntor
Posts: 16
Joined: 2020-03-22T19:04:47-07:00
Authentication code: 1152

Re: font rendering

Post by Suntor »

magick wrote:
2020-03-27T05:19:44-07:00
This means the label must be label:"C:\\\Users\\\Administrator\\\AppData\\\Roaming\\\Microsoft\\\Windows" to render as expected.
I don't think this is true:

Code: Select all

convert
	-font Courier-New -density 96 -pointsize 10 +antialias
	label:"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows"
	label:"C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows"
	label:"C:\\\Users\\\Administrator\\\AppData\\\Roaming\\\Microsoft\\\Windows"
	label:"C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\Microsoft\\\\Windows"
	-append label_backslashes.png
Image
magick wrote:
2020-03-27T04:53:52-07:00
The backslashes are unexpectedly being removed, likely from the string pre-processing.
The problem is not that backslashes are removed. In fact, they are drawn, but incorrectly. As well as other symbols. But for them, on the «Courier New», the effect of incorrect drawing is the worst.

Code: Select all

for /l %i in (8,1,20) do @convert
	-font Courier-New -density 96 -pointsize %i +antialias
	label:"%i C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows"
	path_%i.png
ren path_8.png path_08.png && ren path_9.png path_09.png && convert path_*.png -append path.png && del path_*.png
Image

For comparison, the same «Courier New» font in LibreOffice Writer:
Image
magick wrote:
2020-03-27T06:53:31-07:00
Thanks for the problem report. We can reproduce it and will have a patch to fix it in ...
What about the first topic question about «Microsoft Sans Serif»?

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: font rendering

Post by magick »

Grab the latest beta release of ImageMagick. The mono rendering problem is fixed.

Suntor
Posts: 16
Joined: 2020-03-22T19:04:47-07:00
Authentication code: 1152

Re: font rendering

Post by Suntor »

magick wrote:
2020-03-28T07:04:33-07:00
Grab the latest beta release of ImageMagick. The mono rendering problem is fixed.
I updated the ImageMagick version to 7.0.10.3. The result was much better. Backslashes are drawn, and artifacts on most characters have disappeared. But there were a couple of other problems. Let's try a simple command:

Code: Select all

convert
	-font Microsoft-Sans-Serif -density 96 -pointsize 10 +antialias label:"test.txt"
	test.png
The result is unacceptable. The '.' merged with 't', and the label size was determined incorrectly, which caused the last character to be cropped.
Image

Let's try the different «-pointsize».

Code: Select all

C:\>for /l %i in (8,1,22) do @convert
	-font Microsoft-Sans-Serif -density 96 -pointsize %i +antialias label:"%i C:\\test.txt"
	test_%i.png

C:\>ren test_8.png test_08.png && ren test_9.png test_09.png && convert test_*.png -append test.png && del test_*.png
Characters are merged for half of the «-pointsize» options for the «Microsoft Sans Serif». The label size is always incorrect.
Image


ASCII character test (-annotate +2+15 @ascii.txt) for «Microsoft Sans Serif» font for both 7.0.10.2 and 7.0.10.3 versions.
Image
«Microsoft Sans Serif», ImageMagick 7.0.10.2:
• artifacts, but nothing is shifted;
• need to use «-interline-spacing 0» to match Notepad vertically;
Image
«Microsoft Sans Serif», ImageMagick 7.0.10.3 problems:
• all characters are shifted to the left, except for '#)*,-/;ATVWXY\]_fjtvwxy{}';
• artifacts in drawing the character '8';
• need to use «-interline-spacing 0» to match Notepad vertically;

ASCII character test (-annotate +2+15 @ascii.txt) for «Courier New» font for both 7.0.10.2 and 7.0.10.3 versions.
Image
«Courier New», ImageMagick 7.0.10.2:
• artifacts, but nothing is shifted;
• need to use «-interline-spacing 1» to match Notepad vertically;
Image
«Courier New», ImageMagick 7.0.10.3 problems:
• all characters are shifted to the left, except for 'NV_bmv';
• artifacts in drawing the characters '\' and 'a';
• need to use «-interline-spacing 1» to match Notepad vertically;
Last edited by Suntor on 2020-03-31T08:46:18-07:00, edited 1 time in total.

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: font rendering

Post by magick »

We have a patch for the narrow label (e.g. test.txt). The font rendering itself is problematic as we have three influences: ImageMagick, freetype font rendering API, and the font metrics. Its possible the problem lies with Freetype or the font metrics. We will investigate over the next week or two and see if we can resolve the problem.

Locked