Low level C - Display text, pixel by pixel

2019-04-13 13:30发布

问题:

I am working on a small project where I have to write a low level app. I'd like to display text in that app, and I would even like it to be anti aliased (à la ClearType). No libraries allowed, I have to draw each char pixel by pixel.

What is the best way to do this? Can you recommend some known algorithms? How should I store/read the fonts?

Thanks!

回答1:

You mean you just want to smooth the edges of an existing bitmapped font? This is easy if your original font is 16x32 and you want to render it at 8x16 or something like that, but if you don't have a higher-resolution bitmap to begin with, smoothing is a highly nontrivial operation involving a lot of guesswork. In that case, I would lookup the 2xsai algorithm (which gives visually-pleasing results for this kind of thing) and first perform it to upscale the font to double resolution, then scale it back down with a area-averaging algorithm (i.e. take each destination pixel from the average of a 4-pixel square).

I would also recommend saving your final "anti-aliased" bitmap font and simply using it in your program, rather than performing all this work at runtime.



回答2:

Putting all together:

There are two main types of fonts:

1) Monospaced: all the characters have fixed size, and you define a bitmap for each. No need for Anti Aliasing (you can hardcode the grey levels in the bitmap). Look horrible when resized.

2) True Type: each letter is defined by a set of parameters for Bezier curves. Can be easily scaled to any size, but requires lots of program logic (and processing power!) for that. Anti Aliasing is useful here (and especially the sub-pixel rendering techniquies).

As I see you want to use bitmapped font and rescaling? You could just precompute several of them, thus avoiding complex runtime logic.

As R. suggested, keeping the bitmaps at higher resolution in greyscale instead of BW will help. I'd suggest using size that is divisible by most small numbers, so that the bitmap can be downscaled easily. Also, if this resolution is high enough, then you can keep it in BW and downscale to greyscale (using surface integral).

EDIT: feel free to edit it and please don't vote. Just put all those commentaries together.



回答3:

It is hard to build a good font engine, especially if you need to do scaling and anti-aliasing. So I suggest you take the easy path:

  1. Decide on the fonts and sizes you want to use.
  2. Generate a bitmap font for every font/size combination you need to use. This can be done with a tool like Bitmap Font Generator.
  3. Use the bitmap fonts in your program. Blitting bitmaps should be relatively easy.

If you want more features, I suggest you look into using an engine like FreeType before trying to make your own solution.



回答4:

Well, reading a TTF (or any other) font and rendering some glyph into the bitmap isnt that hard, given you know some stuff about rasterization and bezier curves. The bad point is that if you want the text to look good, it's gonna take a huge amount of code. Aliased font is pretty hard to render, I'm not talking about hinting. There needs to be a routine for kerning, multi-character sequences, something that decides which glyphs map to your characters and also encoding stuff, ...

You might want to use a bitmap font, which comes pre-rendered - then the whole rendering operation is a simple image copy, eventually with some resampling or rotation; but well, you lose the vector font features.

My advice is to take FreeType and live with it, it's a nice library just for this, and can be statically linked and stripped of unnecessary bloat very easily.



标签: c text low-level