Page 1 of 1

Monospaced fonts with IM

Posted: 2010-05-30T00:04:56-07:00
by Gambit
Hi guys -
I'm trying to use IM to render a texture atlas (a constant-width set of tiles, one per printable character) for a old-school RPG game I'm working on. I originally thought I could do something like:

Code: Select all

INPUT=" !\"#$%&'()*+,-./01234\n56789:;<=>?@ABCDEFGHI\nJKLMNOPQRSTUVWXYZ[\\\\]^\n_\`abcdefghijklmnopqrs\ntuvwxyz{|}~"
convert -background none -font ${FONT} -pointsize ${POINTSZ} label:"${INPUT}" ${DEST}.png
The problem, as I'm discovering, is that no matter what font I specify (I've tried 'fixed', 'Courier', 'Lucida-Console-Normal'), the width of the characters is never consistent. I banged together a little test program to show this:

Code: Select all

for i in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
    convert -format $i': %wx%h' -background none -font fixed -pointsize 20 label:"$i" info:
done
With some sample output:

Code: Select all

A: 14x21
B: 13x21
C: 14x21
D: 12x21
E: 12x21
F: 11x21
G: 15x21
H: 12x21
I: 4x21
The interesting thing about this is how thin characters like 'I' are, well, very thin. But nothing about this looks like a monospaced font to me.

Any suggestions on how to generate the image atlas I'm looking for?

Cheers,
--G

Re: Monospaced fonts with IM

Posted: 2010-05-30T08:00:32-07:00
by snibgo
Your test program doesn't specify the required image size, so IM creates the minimum required size to contain the characters without inter-character gaps.

The equivalent of your main command, on Windows 7, IM 6.6.0-8:

Code: Select all

set INPUT=ABCDEFGHI\nabcdefghi\niiiMMMiii\n...---...
set FONT=C:\windows\fonts\cour.ttf
set POINTSZ=20
"%IMG%convert" -background none -font %FONT% -pointsize %POINTSZ% label:"%INPUT%" fixedfont.png
Gives the expected and required result:

Image

Perhaps your command isn't picking up the correct font. Can you post the output?

Re: Monospaced fonts with IM

Posted: 2010-05-30T12:08:15-07:00
by Gambit
Forgot to include version information before:
IM 6.4.0+Win7+Cygwin

Based on your comment wrt inter-character gaps, I tried a slightly different character string:

Code: Select all

INPUT="_ !\"#$%&'()*+,-./01234\n_56789:;<=>?@ABCDEFGHI\n_JKLMNOPQRSTUVWXYZ[\\\\]^\n__\`abcdefghijklmnopqrs\n_tuvwxyz{|}~"
You'll note that the beginning of each fixed width font sample line starts with the same character, an underscore: that produces a max-width character. This sets the initial alignment correct, so that each line begins with a consistent amount of space.

The resulting image is evenly aligned on a grid of 12x17, with one pixel trimmed on both dimensions. I lose the top pixel or so of the backslash and forward slash (fonts are an inexact science, alas) but that's sufficient for my purposes, for now.

Thanks for the thoughts!

Cheers,
--G

Re: Monospaced fonts with IM

Posted: 2010-06-01T18:23:31-07:00
by anthony
Getting to the RPG rendering, Have a look at
Dithering with Symbol Patterns
http://www.imagemagick.org/Usage/quantize/#diy_symbols

This technique does not need all the symbols to be the same size, though for what you want you will probably want to do that.


You may also be interesting in downloading, looking at and modifying the shell script
http://www.imagemagick.org/Usage/generate_symbols

This is an internal script I use to extract specific characters from various fonts,
each character is then trimmed and extended to fit a 20x20 pixel image and then
optionally append a specific image (some letters) on the bottom to generate 20x30
pixel images.

Examples
Image Image Image Image Image Image
Image Image Image Image Image Image Image Image

Each symbol can come from many different fonts, and different character locations.


In your case look for symbolis in the fonts such as "Mincho", which contains the whole UNICODE font set. See Unicode or UTF8 Format Text and Symbol Fonts,

Most of the above from an OpenType Unicode Font "STIXGeneral" that I found on my linux machine. I found it better looking that the thinner looking symbols I found in the Windows "mincho" unicode font.

Also look for the symbol fonts... DingBats, WingDings, WingDings2 WingDings3, WebDings

The images generated are used in IM examples as 'operator' symbols, but you could just as easilly be extracting a range of symbols for DIY Symbol dithering above.