Jump to content
Sign in to follow this  
terjeio

More fonts for RobGs Color LCD graphics library

Recommended Posts

While searching for a font creator I found an interesting one here: http://www.mikrocontroller.net/topic/99701#865331
 
It creates fontdata that are bit aligned so was a bit tricky to render but I have managed to create code that does that.
By default it generates proportional fonts (from True Type) and also includes the top bit set characters that I need for my radio library.
 
So, here is my initial code for rendering fonts generated by this utility - please note it is cannot handle fonts higher than 24 pixes.

 

Add as font.h or to graphics.h:

#include <stdbool.h>

typedef struct Font {
     // common shared fields
       uint16_t   font_Size_in_Bytes_over_all_included_Size_it_self;
       uint8_t    font_Width_in_Pixel_for_fixed_drawing;
       uint8_t    font_Height_in_Pixel_for_all_Characters;
       uint8_t    font_Bits_per_Pixels;
                    // if MSB are set then font is a compressed font
       uint8_t    font_First_Char;
       uint8_t    font_Last_Char;
       uint8_t    font_Char_Widths[1]; // size dependent on number of glyphs
       uint8_t    font_data[];
                    // bit field of all characters
} Font;

uint8_t getFontWidth (Font *font);
uint8_t getFontHeight (Font *font);
inline uint8_t getCharWidth (Font *font, unsigned char c);
uint16_t getStringWidth (Font *font, unsigned char *string);
uint8_t drawFontChar (Font *font, uint16_t x, uint16_t y, unsigned char c, bool opaque);
void drawFontString (Font *font, uint16_t x, uint16_t y, unsigned char *string, bool opaque);

Add to graphics.c - also include font.h if code above not added to graphics.h :

static uint16_t getoffset (Font *font, unsigned char c) {

	uint16_t offset = 0;

	if(c > font->font_First_Char) {
		c -= font->font_First_Char;
		while(c--) {
			offset += font->font_Char_Widths[c];
		};

	}

	return offset;

}

uint8_t getFontWidth (Font *font) {
	return font->font_Width_in_Pixel_for_fixed_drawing;
}

uint8_t getFontHeight (Font *font) {
	return font->font_Height_in_Pixel_for_all_Characters;
}

uint8_t getCharWidth (Font *font, unsigned char c) {

	return c != ' ' && (c < font->font_First_Char || c > font->font_Last_Char) ? 0 : ((c == ' ' || font->font_Char_Widths[c - font->font_First_Char] == 0 ? font->font_Width_in_Pixel_for_fixed_drawing / 4 : font->font_Char_Widths[c - font->font_First_Char]) + 2);
}

uint16_t getStringWidth (Font *font, unsigned char *string) {

	unsigned char c;
	uint16_t width = 0;

	while((c = *string++)) {
		width += getCharWidth(font, c);
	}

	return width;
}

uint8_t drawFontChar (Font *font, uint16_t x, uint16_t y, unsigned char c, bool opaque) {

	uint8_t width = getCharWidth(font, c);
	uint32_t row, column, bitOffset, dataIndex, preShift, pixels;
	bool paintSpace;
	unsigned char *fontData = font->font_Char_Widths + font->font_Last_Char - font->font_First_Char + 1;

	if(width) {

		bitOffset = getoffset(font, c) * font->font_Height_in_Pixel_for_all_Characters;
		dataIndex = bitOffset >> 3;
		preShift = bitOffset - (dataIndex << 3);
		column = width;
		width -= 2;

		paintSpace = c == ' ' || !font->font_Char_Widths[c - font->font_First_Char];

		if(!paintSpace || opaque) {

			x += column;

			while(column--) {

				row = font->font_Height_in_Pixel_for_all_Characters;

				if(!((column == 0) || (column > width)) && !paintSpace) {

					pixels = *((uint32_t *)&fontData[dataIndex]) >> preShift;

					while(row--) {
						if(pixels & 0x01) {
							setArea(x - column, y - row, x - column, y - row);
							writeData(colorHighByte);
							writeData(colorLowByte);
						} else if(opaque) {
							setArea(x - column, y - row, x - column, y - row);
							writeData(bgColorHighByte);
							writeData(bgColorLowByte);
						}
						pixels >>= 1;
					}

					bitOffset += font->font_Height_in_Pixel_for_all_Characters;
					dataIndex = bitOffset >> 3;
					preShift = bitOffset - (dataIndex << 3);

				} else if(opaque) {
					while(row--) {
						setArea(x - column, y - row, x - column, y - row);
						writeData(bgColorHighByte);
						writeData(bgColorLowByte);
					}

				}

			}

		}

	}

	return width == 0 ? 0 : width + 2;

}

void drawFontString (Font *font, uint16_t x, uint16_t y, unsigned char *string, bool opaque) {

	uint32_t xs = x;
	unsigned char c;

	while((c = *string++)) {
		xs += drawFontChar(font, xs, y, c, opaque);
	}

}

The font files has to be modified slightly before they can be used - it should be quite obvious what to remove.

 

Example:

drawFontString((Font *)pixelmix, 5, 100, "The lazy fox...", false);

There are room for optimizations, but I'll leave that for later - my primary goal is to get the DAB project on air.

 

 

Terje

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×