Decoding Microsoft True Type Font Files - c++

I am working on an embedded platform (STM32F407) with a TFT LCD as a display (480x800px) and would like to make my user interface somewhat customizable to the end user. I figured the best source of fonts would be windows compatible as their the most common.
My current implementation uses my own custom drawn font in a binary format and a descriptor table giving the character width and ascii value but having to draw my own font bit by bit is tedious.
I would like to read in a True Type Font file from an SD card and be able to use the different sized glyphs inside it but I have not seen a strait forward implementation on how to actually achieve this magic. Can somebody point me to a good c/c++ example of what I am looking for?
Even better as a way to iron out the kinks I would like to make a simple gcc command line program that will print out my input with a selected font using '#' as pixels. That way I can just worry about implementation and not any other random bugs that might pop up.
Can anybody help me out?

Perhaps you can use the Freetype library.

As duskwuff says: TTF is primarily a vector format, would need to write a renderer. Better off using an image file to define the font, or using a bitmap font format like FNT (Windows) or BDF (UNIX).
Here is my answer to my own question: AngelCode's BMFont & Useage. This makes choosing selective characters from the installed char set, mix in a font and exports an image with a map file to each character. Simple to use.

Related

Adjusting line height in C++

I am attempting to put the final touches on a maze program I have been writing. I have used Unicode to delimit the walls and paths however because of the (horizontal) line spacing I can't quite get it to look compact enough. I have attached two screenshots. I'm just escaping the newline "\n" in order to print each row. Can the distance between lines be adjusted or am I stuck with this "gappy" representation?
My output:
What I am trying to closely represent:
Assuming you aren't printing double newlines, this is outside the scope of standard C++, it does not provide facilities for controlling terminal in a standard way.
Solutions:
You could provide a launcher script, which opens a new terminal window with specific font and runs your app in it.
You could use some platform specific method to change background color (ANSI codes work in unixy terminals, or use Win32 API for Windows terminal, ncurses library on Unix-like environments) and print just spaces in different colors.
Use a GUI library/framework to get complete control on what is drawn (I'd use Qt for C++ GUI app).
TBH if you want pixel-accurate rendering use a proper rendering API, such as OpenGL.
From a text rendering point of view you don't say what you are rendering to. Assuming something like a terminal console or shell window then the layout beyond characters and newlines is nothing to do with your program; the visual representation is entirely determined by the shell you are rendering to.
Firstly, check that you are genuinely printing a line per maze scan line, and not interleaving with spurious newlines. Assuming that is ruled out, the problem is that the unicode glyph is not a full block. So you must somehow set the font or choose another glyph which is a full block.
Usually console windows are 80 characters wide by 22 or 24 characters high, and characters are 8 pixels wide by 19 pixels high. So it's very far from a square grid, and you might want to bias the maze to reflect that and provide a better visual appearance (eg make 2 pixel-wide vertical corridors much more common than 2-pievel wide horizontal corridors).
Do check the binary image library fonts, you might find them useful.
https://github.com/MalcolmMcLean/binaryimagelibrary

OS native 2D API vs OpenGL

Suppose I wanted to create a text editor from scratch.
I searched around and everyone suggested using OS-specific native 2D APIs (e.g. GDI+ in Windows or XLib in Linux), especially for font rendering.
My question is: why is it that openGL isn't suited for such a task? Why is it so hard to render antialiased text and controls as in a text editor with openGL and why should I prefer the non-portable way of native 2D OS APIs?
Part of the difficulty is that OpenGL doesn't provide a font engine or any capabilities specifically for rendering text. In other words, it's not a matter of OpenGL being poorly suited to the rendering part of the task, just that OpenGL is missing a lot of pieces necessary to the task.
To render text under OpenGL, you'd typically start with some font engine to take (for example) a TrueType or OpenType font, and render a glyph from its info (e.g., FreeType). Then you need a text display engine to figure out how to render characters to display your strings decently. In a simple case like English, it has to handle things like kerning and leading. In a complex case like some Arabic scripts, you basically need kind of a feedback loop between the text rendering and the font rendering, because a glyph can take a different form depending on its context in the string.
In short, writing a text editor that renders its text via OpenGL means re-building a lot of a text rendering stack from the ground up.
If you don't care a lot about rendering quality, you might be able to get by with just rendering a few fonts to bitmaps, and displaying your text using them. This can simplify the code quite a bit, but a simple implementation will mean producing output that looks something like an MS-DOS command line. Even matching the output quality of, say, Windows 3.0 will take a fair amount of work. That's not to say it can't be done--but it could dwarf the difficulty of writing the editing part of the text editor.

Drawing bitmap fonts from cp-1252 characters

I'm developping a prototype application to generate bitmap fonts giving TTF.
I'm using Qt library which I'm using for years. However, I realized I've never been involved into "characters" issues before.
What I'm trying is quiet simple:
I need to draw into a PNG file each character of the 1252 codepage. I'm a bit lost with the different issues related to codec, textdecoder etc.
Any suggestion is welcome !
Z.
The codec that you want is QTextCodec::codecForName("Windows-1252")
The characters that you want are char(32) to char(255); put those in a char[225]. Don't forget to zero-terminate them.
Convert that char[225] to a QString with the codec from (1)
Draw the QString from (4)

How do I determine the fallback font for Pango?

I'm rendering some text using pangomm, but the font that I am using doesn't have glyphs for parts of the text (in this case, there is some Japanese mixed in with English). Pango seems to render the text correctly using a fallback font.
How can I determine which font is being used as the fallback?
Actually the font selection is based on the selected Pango font backend. Mostly used (I think) is Fontconfig.
You fonts are basically always chosen by looking at the fonts Unicode coverage, meaning that Fontconfig tries to choose the font that covers the letters in the text you want to render best.
Not knowing if your problem applies to Fontconfig, I won't go into to much detail. But if so, have a look at http://www.freedesktop.org/software/fontconfig/fontconfig-user.html, especially the section on 'font matching'.
Feel free to ask again.

General printing raster and/or vector images

I'm looking for some API for printing.
Basically what I want to achieve is to print set of pixels(monochromatic bitmap which I store in memory) onto the generic paper format (A4,A5..etc.).
What I think that would be minimum API is:
printer devices list
printer buffer where I could send my in-memory pixmap (ex. like winXP printer tasks folder)
some API which would translate SI dimensions onto printer resolution, or according to previous - in memory pixmap (ex. 450x250) onto paper in appropriate resolution.
What I was considering is postScript, but I've some old LPT drived laserjet which probably doesn't support *PS.
Currently I'm trying to find something interesting in Qt - QGraphicsView.
http://doc.trolltech.com/4.2/qgraphicsview.html
Well you got close, look at Printing in Qt. There is the QPrinter class that implements some of what you are looking for. It is implmenetent as a QPaintDevice. This means that any widget that can render itself on the screen can be printed. This also mean you don't need to render to a bitmap to print, you can use Qt widgets or drawing functions for printing
On a side note, check the version number of the Qt documentation, the last release of Qt is 4.5, 4.6 is in beta.
You might want to investigate wx python for printing. Learning the framework might be a bit of an overhead for you though! I've had success with that in the past, both on windows and linux.
I've also used reportlab to make PDFs which are pretty easy to print using the minimum of OS interaction.
I would use PIL to create a BMP file, and then just use the standard OS services to print that file. PIL will accept data in either raster or vector form.