0

There is a known common approach to store the fonts definition as per:

char fontImg[][FONTW * FONTH] = {
    { // ' ' (space)
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0  
    },

Is there any better/more c++ approach to do this? I hate mixing C / C++ code and i avoid it wherever possible, so instead of working with an array, does anybody know what else could be used to store a definition of fonts ?

std::vector
std::map

anything else?

A quick idea:

// Change to class later
struct tCharacter
{
    uint8_t m_uiSizeX;
    uint8_t m_uiSizeY;
    std::string m_strData;
};

std::map<uint8_t,tCharacter> m_mFonts;



tCharacter _Char;
// 0 empty pixel, 1 filled pixel, # 'return carret'
/*
 00111100#
 01000010#
 01000010#
 01000010#
 01111110#
 01000010#
 01000010#
 01000010
 */
_Char.m_strData="00111100#01000010#01000010#01000010#01111110#01000010#01000010#01000010";
m_mFonts['a']=_Char;
PeeS
  • 1,164
  • 3
  • 20
  • 43
  • 2
    Is there an actual problem you're trying to solve? – Lightness Races in Orbit Oct 08 '15 at 18:01
  • Why would you want to replace a simple, fixed array with all of the overhead of a Vector or Map? What functionality from either of those would you need to hold a font definition? This is a clear case of *let's add lots of overhead and clutter to something simple because I want to use OOP*. – Ken White Oct 08 '15 at 18:01
  • this is just an idea question, i don't mind to use the standard C - just out of curiosity. – PeeS Oct 08 '15 at 18:03
  • A common implementation in embedded systems is to declare the bytes without using 2d notation. The bitmap should also be declared as `static const` because there will only be 1 instance and the font bitmap won't be modified. – Thomas Matthews Oct 08 '15 at 18:56
  • By the way, please refer to the fonts as *bitmap* fonts to denote the difference between scalable fonts. Also, is your font fixed pitch or variable width? – Thomas Matthews Oct 08 '15 at 18:59
  • @KenWhite - what's wrong with saying: i want to do it OOP ? i find a lot of sense in that approach. ThomasMatthews thanks, i have updated the subject. – PeeS Oct 08 '15 at 19:02
  • Still i didn't see any example of other approach. Any proposal in OOP ? – PeeS Oct 08 '15 at 19:05
  • 1
    Seems like a fairly reasonable question - however I would prefer to store this as a string like "0100010100101001..." saves a lot of typing. Even better is to load such data from a file. – FreelanceConsultant Oct 08 '15 at 19:19
  • @user3728501 i totally agree, went with std::string for a single glyph description (as in example in the question) std::map will allow to define fonts with a simple text file: a 010100010100101... b 10000100001000... c 0001000010000 as i will be mapping the first (a,b,c...) to the key straight away – PeeS Oct 08 '15 at 19:34
  • @PeeS I use array of `DWORD`s or **Bitmap** for platforms without font present. See [How to convert from alphabets to pixels](http://stackoverflow.com/a/20468992/2521214). – Spektre Oct 10 '15 at 06:36

0 Answers0