Andy A / miniFont

Dependents:   WS2812Text

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers miniFont.cpp Source File

miniFont.cpp

00001 #include "miniFont.h"
00002 
00003 const char miniFont::dot[5] = { 0x00,0x00,0x00,0x03,0x03};
00004 const char miniFont::space[5] = { 0x00,0x00,0x00,0x00,0x00};
00005 const char miniFont::exclam[5] = { 0x01,0x01,0x01,0x00,0x01};
00006 const char miniFont::quest[5] = { 0x0f,0x09,0x02,0x00,0x02};
00007 
00008 const char miniFont::numbers[10][5] = {
00009     { 0x0e, 0x11, 0x11, 0x11, 0x08 }, // 0
00010     { 0x06, 0x02, 0x02, 0x02, 0x07 }, // 1
00011     { 0x1f, 0x01, 0x06, 0x18, 0x1f }, // 2
00012     { 0x1f, 0x01, 0x0f, 0x01, 0x1f }, // 3
00013     { 0x10, 0x10, 0x14, 0x1f, 0x04 }, // 4
00014     { 0x1f, 0x10, 0x1e, 0x01, 0x1e }, // 5
00015     { 0x1f, 0x10, 0x1f, 0x11, 0x1f }, // 6
00016     { 0x1f, 0x01, 0x01, 0x02, 0x02 }, // 7
00017     { 0x1f, 0x11, 0x08, 0x11, 0x1f }, // 8
00018     { 0x1f, 0x11, 0x1f, 0x01, 0x1f } // 9
00019 };
00020 
00021 const char miniFont::letters[26][5] = {
00022     { 0x1f, 0x11, 0x1f, 0x11, 0x11 }, // A
00023     { 0x1e, 0x11, 0x1e, 0x11, 0x1e }, // B
00024     { 0x1f, 0x10, 0x10, 0x10, 0x1f }, // C
00025     { 0x1e, 0x11, 0x11, 0x11, 0x1e }, // D
00026     { 0x1f, 0x10, 0x1e, 0x10, 0x1f }, // E
00027     { 0x1f, 0x10, 0x1e, 0x10, 0x10 }, // F
00028     { 0x1f, 0x10, 0x13, 0x11, 0x1f }, // G
00029     { 0x11, 0x11, 0x1f, 0x11, 0x11 }, // H
00030     { 0x07, 0x02, 0x02, 0x02, 0x07 }, // I
00031     { 0x01, 0x01, 0x01, 0x11, 0x1f }, // J
00032     { 0x11, 0x12, 0x1c, 0x12, 0x11 }, // K
00033     { 0x10, 0x10, 0x10, 0x10, 0x1f }, // L
00034     { 0x11, 0x1b, 0x15, 0x11, 0x11 }, // M
00035     { 0x11, 0x19, 0x15, 0x13, 0x11 }, // N
00036     { 0x1f, 0x11, 0x11, 0x11, 0x1f }, // O
00037     { 0x1f, 0x11, 0x1f, 0x10, 0x10 }, // P
00038     { 0x1f, 0x11, 0x15, 0x1f, 0x04 }, // Q
00039     { 0x1f, 0x11, 0x1f, 0x12, 0x11 }, // R
00040     { 0x1f, 0x10, 0x1f, 0x01, 0x1F }, // S
00041     { 0x1f, 0x04, 0x04, 0x04, 0x04 }, // T
00042     { 0x11, 0x11, 0x11, 0x11, 0x1F }, // U
00043     { 0x11, 0x11, 0x11, 0x0a, 0x04 }, // V
00044     { 0x11, 0x11, 0x15, 0x1b, 0x11 }, // W
00045     { 0x11, 0x0a, 0x04, 0x0a, 0x11 }, // X
00046     { 0x11, 0x11, 0x1f, 0x04, 0x04 }, // Y
00047     { 0x1f, 0x02, 0x04, 0x08, 0x1f } // Z
00048 };
00049 
00050 
00051 miniFont::miniFont() {
00052 
00053     rotate90 = false;
00054     fixedWidth = false;
00055      }
00056 
00057 
00058 uint8_t miniFont::getMinPixWidth(char letter)
00059 {
00060     switch(letter) {
00061         case '.':
00062             return 2;
00063         case ' ':
00064         case '1':
00065         case 'I':
00066         case 'i':
00067             return 3;
00068         case '!':
00069             return 1;
00070         case '?':
00071             return 4;
00072         default:
00073             return maxWidth;
00074     }
00075 }
00076 
00077 
00078 uint8_t miniFont::getPixWidth(char letter)
00079 {
00080     if (fixedWidth)
00081         return maxWidth;
00082     else
00083         return getMinPixWidth(letter);
00084 }
00085 
00086 uint8_t miniFont::getPixHeight(char letter)
00087 {
00088     return 5;
00089 }
00090 
00091 // gets the proportional letter
00092 bool miniFont::getRawChar(char letter, const char **rowArray)
00093 {
00094 
00095     if ((letter>='A') && (letter<='Z')) { // upper
00096         *rowArray = letters[letter-'A'];
00097         return true;
00098     }
00099     if ((letter>='a') && (letter<='z')) { // lower
00100         *rowArray = letters[letter-'a'];
00101         return true;
00102     }
00103     if ((letter>='0') && (letter <='9')) {
00104         *rowArray = numbers[letter-'0'];
00105         return true;
00106     }
00107     if (letter == '.') {
00108         *rowArray = dot;
00109         return true;
00110     }
00111     if (letter == '!') {
00112         *rowArray = exclam;
00113         return true;
00114     }
00115     if (letter == '?') {
00116         *rowArray = quest;
00117         return true;
00118     }
00119     if (letter == ' ') {
00120         *rowArray = space;
00121         return true;
00122     }
00123     return false;
00124 }
00125 
00126 // converts to fixed width if needed.
00127 bool miniFont::getVerticalChar(char letter, const char **rowArray)
00128 {
00129     if (!fixedWidth)
00130         return getRawChar(letter,rowArray);
00131 
00132     uint8_t shift = (maxWidth - getMinPixWidth(letter)) / 2;
00133     if (shift == 0)
00134         return getRawChar(letter,rowArray);
00135 
00136     const char *rawletter;
00137     if (getRawChar(letter,&rawletter)) {
00138         for (int i = 0; i < maxHeight; i++) {
00139             letterBuffer[i] = rawletter[i]<<shift;
00140         }
00141         *rowArray = (const char *)letterBuffer;
00142         return true;
00143     }
00144     return false;
00145 }
00146 
00147 // rotates if needed.
00148 bool miniFont::getChar(char letter, const char **rowArray)
00149 {
00150     if (!rotate90) return getVerticalChar(letter,rowArray);
00151 
00152     const char *vertical;
00153     if (!getVerticalChar(letter,&vertical))
00154         return false;
00155 
00156     for (int i = 0; i < maxWidth; i++) {
00157         rotateBuffer[i] = 0;
00158     }
00159 
00160     for (int i = 0; i < maxHeight; i++) {
00161         for (int j = 0; j < maxWidth; j++) {
00162             rotateBuffer[i] = vertical[j] << j;
00163         }
00164     }
00165     *rowArray = vertical;
00166     return true;
00167 }