Using fonts/text in a custom png library (C++)

Red Squirrel

No Lifer
May 24, 2003
70,214
13,605
126
www.anyf.ca
I wrote a custom png class that uses libpng directly, as the others I used were cubbersome to deploy and would ring me through dependency hell. So to do way with that I just went straight to using libpng, which is very standard and less likely to cause issues. I have my own pixel matrix system, and have basic functionality such as plotting lines, dots, alpha channels, inserting pictures etc...

How would I go about implementing fonts and text? Basically, how do I read a font file and get the pixel matrix of a character at a certain size/format (italic etc) so that I can implement it in my class?

I want to do this without needing any libraries. Or at very least no 3rd party ones. Standard ones that will be the same on all systems like libpng I can deal with but if I can avoid it even better. Basically my code needs to work on any system without having to fight with libraries or complex g++ strings that could change between systems. This is a universal helper set of classes that I can drop in any program without having to deal with any dependency issues so coding it without use of libraries is important. On a app to app basis I'll use libraries if I need to, but I want to keep my base "library" clean of any dependencies.

I'm also thinking I might just make my own built in font for the class. If I want to do anything fancier in a specific program I can always use libs in that case. I just don't want to use libs for this base class.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,634
4,562
75
Here's some font images you might find useful: http://www.hassings.dk/lars/fonts.html Just insert the appropriate part of the picture.

If you need a resizable font, that's more complicated. If you need multiple resizable fonts, you should probably just use the libraries. Although maybe you could look at implementing your own reading of SVG font files. (How are you at drawing Bézier curves?)
 

Red Squirrel

No Lifer
May 24, 2003
70,214
13,605
126
www.anyf.ca
Was thinking of doing that too actually, just have a grid of premade characters that can be assigned to each char value. Basically I load it in my class instance then when I apply text it uses that. Could apply multiple font matrixes to an image and use them. My class already handles png transparancy so I could make the matrixes have a transparent background as well.

If I am writing a program where I need to use standard fonts then I can always use a library for that specific program.

Is there actually a standard for font matrixes like the ones on that site and a place I can download more? Or is there an easy way I can generate a consistent one using another program using a regular font? Ex: perhaps using some kind of script in Gimp? Basically if I can establish a standard format it will make my code easier to write as I can just specify the matrix file, the start pixel, then the size of each char, then it loops through a standard way (ex: the standard would predefine that there is nn characters per row and they start at ascii value 1 to 255) I imagine I can always write a separate app using a library that will do that part for me though.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,634
4,562
75
Is there actually a standard for font matrixes like the ones on that site and a place I can download more?
I didn't find any, but I may have missed something.
Or is there an easy way I can generate a consistent one using another program using a regular font? Ex: perhaps using some kind of script in Gimp?
I don't know GIMP scripting, but I imagine I could do something with Selenium and automatic screenshotting.

In general I'd say an 8x12 grid of characters 32-127 would be best. (Get row with AND, and column with a bitshift!) Of course, this won't work well for variable-width fonts. The other problem is this doesn't cover Unicode at all. And I can't imagine covering even a small fraction of Unicode this way.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Font rendering is not very straightforward, there are all sorts of gotchas that will make your text hard to read or downright broken if you don't do it correctly. I would highly recommend reading through the freetype documentation. At least the parts that describe fonts, what all the metrics mean in the font files, and how to properly render them.

Even if you don't use freetype to load your fonts(not sure why you wouldn't though, they have done all the hard work already) it will teach you all the intricacies of fonts and how to render them properly.
 

Red Squirrel

No Lifer
May 24, 2003
70,214
13,605
126
www.anyf.ca
Ended up going the freetype route, I normally don't like using libraries as you end up with dependency hell when trying to compile the app on various machines but considering freetype is more than likely going to be standard install on every machine, and I'm already depending on libpng anyway which is also typically going to always be installed, I probably should be fairly safe to not run into dependency hell issues. The process to implement the lib is nice as it uses the freetype-config command to determine the lib path, so I don't have to worry about paths changing on me if I install the app on another system then have to figure out where they are etc. The command does it and can go right in the g++ command directly. Actually I just realized now that's what libpng does too. So these libs should hopefully be fairly easy to use from system to system without running into dependency or path issues etc.

I did not get the actual code to work yet but going through the tutorial and examples, and so far so good. Once I get it to work separately I'll then implement it in my class.

Just for future reference and if someone else runs across this, this is the compile string (with png) :

Code:
g++ -g -o pngtest2 pngtest2-freetype.cpp  `libpng-config --ldflags` `freetype-config --cflags` `freetype-config --libs`