How to convert a bitmap font (.FON) into a truetype font (.TTF)?

拜拜、爱过 提交于 2019-11-26 12:55:02

问题


My program (win32, Delphi) needs to display special chars in some columns of a table. To do that I use a special font for those columns. I got the font from my client. It is a .FON font. It works good on the screen but I often get problems as soon as I want to use it to print something.

I would like to convert this .FON font into to truetype font (.TTF) to avoid the problems. I don\'t care if the font does not scale good. I should just looks exactly the same when used on the screen with the same size as the default size of the original font. Do someone know a way to do that?

(It don\'t necessary need a source code solution. The font won\'t change. It\'s enough if I find a tool to do it)

Edit: Ideal would be to get a truetype font where each pixel of the original font is converted into a vectorial black square (I tested by redrawing a few chars manually, it would works as I want).

Edit 2, solution used: Using FontForge + Autotrace and then making corrections manually I was able to get a vector font that follow the outline of the pixels of the bitmap font. This vector font scales somewhat better than the original font and solve my printing problems. See accepted post for details.

But I\'m still interested if someone knows a fully automated solution.


回答1:


Use FontForge + Autotrace then make some corrections manually.

First integrate Autotrace in Fontforge, see Autotracing bitmaps in FontForge. Here is how I have done it, as I wasn't able to use the win32 version of FontForge:

  1. Download, unzip and start Portable Ubuntu Tres. It's a special linux version that works under Windows!
  2. Choose System->Administration->Add/Remove Applications, search for FontForge and install it (administrator password is 123456). I got FontForge version 20090622 installated.
  3. Download autotrace-0.31.1.tar.gz from http://autotrace.sourceforge.net/index.html#download and copy it in your home directory in Portable Ubuntu.
  4. In Portable Ubuntu choose Applications->Accessories->Terminal to compile and install autotrace:

    sudo bash  (password is 123456)
    gunzip autotrace-0.31.1.tar.gz
    tar xf autotrace-0.31.1.tar
    cd autotrace-0.31.1
    ./configure
    make
    make install
    

Then convert your font:

  1. Start FontForge in Portable Ubuntu and open the .FON font.
  2. Save your font as a .BDF font (Loading a .FON file as background didn't work for me so I had to convert the font first): File->Generate Fonts and choose BDF as format.
  3. Create a new font and import the BDF Font as background font: File->Import and don't forget select "As Background".
  4. Select all characters and start an autotrace while holding the shift key so that you can enter parameters for autotrace. I used following parameters, which improved the recognition of the outline of the pixels but I didn't found the perfect parameters:

    -corner-surround=1 -line-threshold=10
    
  5. Correct the chars manually using mostly the function "Add a curve point". Activating View->Show->Almost Horizontal/Vertical Lines makes the work much easier.
  6. Generate a truetype font: File->Generate Fonts and choose truetype as format.



回答2:


I know this question is old, but for what it is worth I was able to easily convert an old .fon to .ttf using PixFont. I ran it as an administrator (no installs - an old school executable in place) and it generated a good TTF even though the original .fon was missing some correct glyphs.

And, while it isn't free, at $29 it almost is, especially compared to the very expensive commercial products out there.




回答3:


For those that do not want to compile fontforge (for windows) grab a download here: http://www.mpetroff.net/software/fontforge-windows/

That build uses potrace instead of autotrace. The best parameters I got for the font that I was converting (vgasys.fon) were:

--turdsize 0 --opttolerance 0 --unit 1 --longcurve --gamma 0 --blacklevel 0 --cleartext

It was still hours of manual work to get the font looking good (by adding and removing points).




回答4:


Using FontForge I was able to generate a corresponding truetype font, which gives exactly the same chars as the original font when used at the good size. May be I still need to tweak some options, because I used my original font at size=11. The new font works good only at size=10.5, which is impossible to use in delphi. The font looks really horrible at every other size.

  1. Load the .FON font in FontForge
  2. Choose File->Generate Fonts
  3. Select "(faked) MS bitmap only sfnt (ttf)" as type, entre a file name and click "save".

Apparently it uses a possibility to store bitmap fonts in a truetype-font. The resulting font doesn't have the problems I had with the bitmap fonts, but it is completly blank on the printer so that it doesn't solve my problem.

A note about FontForge: I didn't manage to install the Windows Version of FontForge (based on cygwin). Instead of that I Installed Portable Ubuntu Tres, which is a linux version that works on Windows. It's easy to install: unzip the file and start the exe. Then choose System->Administration->Add/Remove Applications and search for FontForge and install it (administrator password is 123456). I got FontForge version 20090622 installated.




回答5:


I found that the easiest route to getting a good TTF version of a FON is to use one of the free online bitmap font editors. You have to manually enter each glyph, but the process is much simpler than coming to grips with FontForge. There are two editors that I know of, and they both produce TTFs that work fine on Windows and Linux.

BitFontMaker is the simpler of the two. I like its clean interface, but I ended up not using it because monospaced fonts can only have cell widths of 10 to 14 pixels.

FontStruct requires registration and uses a Flash interface. I was able to make new fonts quite fast once I got used to the interface. (It might be coincidence, but I noticed an uptick in spam on the email account I used to register.)

Both editors set the line spacing automatically. A trick to get the spacing you want is to add a high pixel on one character, then use a vector font editor to remove it from the TTF. (Type light works well and is much easier to use than FontForge.)




回答6:


A possibility might be to use the command line tool SBIT32 fom Microsoft. It's easy to convert the .FON file into a .BDF file using FontForge. But then you need to write a metrics file (.MET). I didn't try it, because I suppose that I would get a truetype-font with embedded bitmaps font, and it is possible to generate such fonts directly with FontForge (type "(faked) MS bitmap only sfnt (ttf)" under File->Generate Fonts).




回答7:


According to the product description BitFonter 3.0 seems to be able to do that:

"Powerful conversion of bitmaps and bitmap fonts from and to Type 1, TrueType and OpenType fonts through integration with FontLab Studio and TypeTool."

But US $999 is a little bit expensive to convert just one font.




回答8:


I found myself searching for these fonts to use with a project I am working on. Cringing at the thought of manually editing all of the glyphs as mentioned in the other answers, I continued to search. Eventually I stumbled across a site which has the original font as well as some newer .ttf variants. If you don't care that some of the glyphs have been remapped (they are all still present) this is a nice alternative: Oldschool PC Fonts. As an added bonus for me these support extended Latin, Greek, Cyrillic and Hebrew scripts plus a bunch of additional glyphs and Unicode symbols.




回答9:


It is unlikely that you can accomplish it with a simple free tool. http://www.fontlab.com have tools to do it, but they are very expensive.




回答10:


I couldn't get autotrace to work, so I wrote A python script to trace black and white images into vector polygons, one for each pixel.

So with a folder of glyph images, you can convert it into a folder of svg files.

Then in Inkscape union + simplify path to clean up each glyph. ( might be doable on command line with Inkscape)

Finally import the corresponding svg into FontForge for each glyph.

-> script hosted at gist.gisthub.com

from PIL import Image
import svgwrite
import os
import argparse


def bmp_to_svg(filename, multiply =1):

    img = Image.open(filename)
    w, h = img.size
    pixel_acc = img.load()

    generated_paths = paths_gen(pixel_acc, w,h, multiply=multiply)

    dwg = svgwrite.Drawing(profile='tiny')
    for path in generated_paths:
        options = path
        kwoptions = {} #{"fill":"black"}
        dwg.add(dwg.polygon(options, **kwoptions))

    #clip_path = dwg.defs.add(dwg.clipPath())
    #clip_path.add(dwg.Rect(insert=(0,0), size=((w*multiply)+1, (h*multiply)+1)))
    return dwg

def paths_gen(pixel_acc, w, h, multiply = 1):
    m = multiply
    paths = []
    for y in range(0,h):
        for x in range(0,w):
            pix = pixel_acc[x,y]
            brightval = sum(pix) /3
            if brightval < 128:
                paths.append([
                        ##top left
                        (x * m, y * m),
                        ##top right
                        ((x+1) * m, y * m),
                        ##bottom right
                        ((x+1) * m, (y+1) * m),
                        ##bottom list
                        ((x) * m, (y+1) * m),
                        ##close back to top left
                        #(x, y), 
                    ])
    return paths


def main():
    parser = argparse.ArgumentParser(
        description='Script to convert black and white images into vector art')
    parser.add_argument("input_dir")
    parser.add_argument("ouput_dir")
    parser.add_argument("in_ext", default="bmp", help="file extension of images in input folder")
    parser.add_argument("-m", "--multiply", default=1, type=int)
    ## TODO
    #parser.add_argument("-u", "--union", store=True, help="unifies adjecent polgons")
    #parser.add_argument("-s", "--simplify", store=True, help="removes points that are part of a straight line")
    #parser.add_argument("-c", "--compress", store=True, help="same as '--union --simplify'")

    args = parser.parse_args()


    cdir = os.path.abspath(args.input_dir)

    fn_gen = ( x for x in os.listdir(cdir)
                 if x.endswith('.'+args.in_ext)
             )

    for count, filename in enumerate(fn_gen):

        full_filename =  os.path.join(cdir, filename)

        new_fn = "{}_.svg".format(filename)
        new_fullfn =  os.path.join(args.ouput_dir, new_fn)


        svg = bmp_to_svg(full_filename, multiply=args.multiply)
        print "Converted file: '{}'".format(full_filename)
        svg.filename = new_fullfn
        svg.save()

if __name__ == '__main__':
    main()



回答11:


I had to go through a lot of hassle to find the solution that worked for me.

Autotrace seems to produce a pixel perfect output, if the input is scaled, from my experience scaling at least 8x works perfectly.

Scaling the input is overcomplicated thanks to the software being closed.

Bitfonter can open bdf fonts (which do support Unicode) and it can scale. The problem with the demo version is that half of the glyphs are "corrupted" when exporting (pixels randomly removed) and this makes Autotrace again produce an undesirable output. But, I noticed there is a pattern to which characters are "corrupted": the first and second glyphs are corrupted, the third and fourth are not, the fifth and sixth are corrupted, and so on. So, I made a complementary bdf file by again scaling it but then removing first two glyphs. Then, combining these two can be done in FontForge: open both of those output files, "Reencode" in "Encoding" to "Glyph Order", select all in output2 and copy, in output1 click the third glyph and press Ctrl+Shift+V. This will combine both output files and make all characters scaled perfectly uncorrupted, except for the first two glyphs which can be done manually. Save the result.

Perform the "Autotrace" on this scaled output. Then, on all glyphs, Element\Transformations\Transform, then use the following settings: Origin: Glyph Origin, Scale Uniformly... the inverse of what you scaled the bdf before (in case of 8x scaling that's 12.5%).

There are more points than needed, this can be resolved by using Element\Simplify\Simplify, this will simplify the outlines. In this specific case of the outlines being made of only horizontal and vertical lines, this operation should be lossless.

Then, after all this, I noticed that the glyphs seemingly don't have Unicode information. Open whatever font has exactly the characters of the resulting font, use Encoding\Reencode\ISO 10646-1 (Unicode, BMP) on that font, then use Compact. Then copy all of the characters from the font that has all of the right outlines onto the font that has all of the right Unicode encodings.

If you cannot follow a step, let me know what is happening. This really is overcomplicated and it was a ton of hassle trying to use and combine the effects of every single tool I could find, but I found the solution. It's automated, making it easier for fonts with tons of characters than redrawing all the characters manually. This should help for anyone who drew bitmap glyphs and has a valid bitmap font file but cannot use it yet.



来源:https://stackoverflow.com/questions/3750124/how-to-convert-a-bitmap-font-fon-into-a-truetype-font-ttf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!