Page 1 of 1

Fonts with space defined

Posted: Sat Jan 18, 2014 10:54 am
by Brissieboy
Some fonts do not define the space (0x32) character with the assumption that it is a blank character of 'n' width - the same width as the 'n' character. The DMD library assumes that this is the case so that any definition of the space character created within a font is ignored and a blank 'n' space is used.
I wanted to be able to define the space character within my own character set so needed to modify the library accordingly by changing the DMD::charWidth() method in DMD.cpp.
Here is my version

Code: Select all

byte DMD::charWidth(const char letter)
{
    uint8_t width = 0;

    uint8_t firstChar = pgm_read_byte(this->Font + FONT_FIRST_CHAR);
    uint8_t charCount = pgm_read_byte(this->Font + FONT_CHAR_COUNT);

    uint16_t index = 0;

    char c = letter;
    // Space is often not included in font so use width of 'n' if this is the case
    if (c == ' ') {

        if (pgm_read_byte(this->Font + FONT_LENGTH) != 0
	    || pgm_read_byte(this->Font + FONT_LENGTH + 1) != 0) {
	    // non-zero length is variable width font so check to see is space width specified

            if (pgm_read_byte(this->Font + FONT_WIDTH_TABLE + (c - firstChar)) == 0) { // is 0 if space width not specified
                c = 'n';
            }
        }

        else c = 'n'; // make space the same size as 'n'
    }

    if (c < firstChar || c >= (firstChar + charCount)) {
	    return 0;
    }
    c -= firstChar;

    if (pgm_read_byte(this->Font + FONT_LENGTH) == 0
	&& pgm_read_byte(this->Font + FONT_LENGTH + 1) == 0) {
	    // zero length is flag indicating fixed width font (array does not contain width data entries)
	    width = pgm_read_byte(this->Font + FONT_FIXED_WIDTH);
    } else {
	    // variable width font, read width data
	    width = pgm_read_byte(this->Font + FONT_WIDTH_TABLE + c);
    }
    return width;
}
This now uses the 'n' width if the space is not defined, but the defined width if it is specified.