1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**     \file   Font_08px.c

        \brief  A readable 8 pixel height, proportional font.

        \license 
                MIT: The MIT License (https://opensource.org/licenses/MIT)
                .
                Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
                and associated documentation files (the "Software"), to deal in the Software without restriction, 
                including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
                and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
                subject to the following conditions:
                .
                The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
                .
                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
                INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
                IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
                WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
                THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
        \copyright
                DIT: 2017; Drechsler Information Technologies; www.drechsler-it.de
 
        \authors
                jrgdre: Joerg Drechsler; DIT
 
        \versions
                1.0.0: 2017-07-01 jrgdre initial release

*/

#include "Font.h"
#include "Font_08px.h"

#define FONT_08PX_CHAR_IDX_MAX          95
#define FONT_08PX_CHAR_WIDTH_BYTE_IDX    8
#define FONT_08PX_HEIGHT                 8

/**
 * Character index = (ASCII-Code - 0x20)
 */
const uint8_t font_08px[FONT_08PX_CHAR_IDX_MAX + 1][FONT_08PX_HEIGHT + 1] = {
        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},        // Char 032 ( )
        {0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0xC0, 0x00, 0x02},        // Char 033 (!)
        {0xD8, 0xD8, 0xD8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05},        // Char 034 (")
        {0x6C, 0x6C, 0xFE, 0x6C, 0xFE, 0x6C, 0x6C, 0x00, 0x07},        // Char 035 (#)
        {0x18, 0x7E, 0xC0, 0x7C, 0x06, 0xFC, 0x18, 0x00, 0x07},        // Char 036 ($)
        {0x00, 0xC6, 0xCC, 0x18, 0x30, 0x66, 0xC6, 0x00, 0x07},        // Char 037 (%)
        {0x38, 0x6C, 0x38, 0x76, 0xDC, 0xCC, 0x76, 0x00, 0x07},        // Char 038 (&)
        {0x60, 0x60, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03},        // Char 039 (')
        {0x30, 0x60, 0xC0, 0xC0, 0xC0, 0x60, 0x30, 0x00, 0x04},        // Char 040 (()
        {0xC0, 0x60, 0x30, 0x30, 0x30, 0x60, 0xC0, 0x00, 0x04},        // Char 041 ())
        {0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00, 0x08},        // Char 042 (*)
        {0x00, 0x30, 0x30, 0xFC, 0x30, 0x30, 0x00, 0x00, 0x06},        // Char 043 (+)
        {0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60, 0xC0, 0x03},        // Char 044 (,)
        {0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x06},        // Char 045 (-)
        {0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x02},        // Char 046 (.)
        {0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x80, 0x00, 0x07},        // Char 047 (/)
        {0x7C, 0xCE, 0xDE, 0xF6, 0xE6, 0xC6, 0x7C, 0x00, 0x07},        // Char 048 (0)
        {0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xFC, 0x00, 0x06},        // Char 049 (1)
        {0x7C, 0xC6, 0x06, 0x7C, 0xC0, 0xC0, 0xFE, 0x00, 0x07},        // Char 050 (2)
        {0xFC, 0x06, 0x06, 0x3C, 0x06, 0x06, 0xFC, 0x00, 0x07},        // Char 051 (3)
        {0x0C, 0xCC, 0xCC, 0xCC, 0xFE, 0x0C, 0x0C, 0x00, 0x07},        // Char 052 (4)
        {0xFE, 0xC0, 0xFC, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x07},        // Char 053 (5)
        {0x7C, 0xC0, 0xC0, 0xFC, 0xC6, 0xC6, 0x7C, 0x00, 0x07},        // Char 054 (6)
        {0xFE, 0x06, 0x06, 0x0C, 0x18, 0x30, 0x30, 0x00, 0x07},        // Char 055 (7)
        {0x7C, 0xC6, 0xC6, 0x7C, 0xC6, 0xC6, 0x7C, 0x00, 0x07},        // Char 056 (8)
        {0x7C, 0xC6, 0xC6, 0x7E, 0x06, 0x06, 0x7C, 0x00, 0x07},        // Char 057 (9)
        {0x00, 0xC0, 0xC0, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x02},        // Char 058 (:)
        {0x00, 0x60, 0x60, 0x00, 0x00, 0x60, 0x60, 0xC0, 0x03},        // Char 059 (;)
        {0x18, 0x30, 0x60, 0xC0, 0x60, 0x30, 0x18, 0x00, 0x05},        // Char 060 (<)
        {0x00, 0x00, 0xFC, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x06},        // Char 061 (=)
        {0xC0, 0x60, 0x30, 0x18, 0x30, 0x60, 0xC0, 0x00, 0x05},        // Char 062 (>)
        {0x78, 0xCC, 0x18, 0x30, 0x30, 0x00, 0x30, 0x00, 0x06},        // Char 063 (?)
        {0x7C, 0xC6, 0xDE, 0xDE, 0xDE, 0xC0, 0x7E, 0x00, 0x07},        // Char 064 (@)
        {0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0x00, 0x07},        // Char 065 (A)
        {0xFC, 0xC6, 0xC6, 0xFC, 0xC6, 0xC6, 0xFC, 0x00, 0x07},        // Char 066 (B)
        {0x7C, 0xC6, 0xC0, 0xC0, 0xC0, 0xC6, 0x7C, 0x00, 0x07},        // Char 067 (C)
        {0xF8, 0xCC, 0xC6, 0xC6, 0xC6, 0xCC, 0xF8, 0x00, 0x07},        // Char 068 (D)
        {0xFE, 0xC0, 0xC0, 0xF8, 0xC0, 0xC0, 0xFE, 0x00, 0x07},        // Char 069 (E)
        {0xFE, 0xC0, 0xC0, 0xF8, 0xC0, 0xC0, 0xC0, 0x00, 0x07},        // Char 070 (F)
        {0x7C, 0xC6, 0xC0, 0xC0, 0xCE, 0xC6, 0x7C, 0x00, 0x07},        // Char 071 (G)
        {0xC6, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xC6, 0x00, 0x07},        // Char 072 (H)
        {0xFC, 0x30, 0x30, 0x30, 0x30, 0x30, 0xFC, 0x00, 0x06},        // Char 073 (I)
        {0x06, 0x06, 0x06, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x07},        // Char 074 (J)
        {0xC6, 0xCC, 0xD8, 0xF0, 0xD8, 0xCC, 0xC6, 0x00, 0x07},        // Char 075 (K)
        {0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xFE, 0x00, 0x07},        // Char 076 (L)
        {0xC6, 0xEE, 0xFE, 0xFE, 0xD6, 0xC6, 0xC6, 0x00, 0x07},        // Char 077 (M)
        {0xC6, 0xE6, 0xF6, 0xDE, 0xCE, 0xC6, 0xC6, 0x00, 0x07},        // Char 078 (N)
        {0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x07},        // Char 079 (O)
        {0xFC, 0xC6, 0xC6, 0xFC, 0xC0, 0xC0, 0xC0, 0x00, 0x07},        // Char 080 (P)
        {0x7C, 0xC6, 0xC6, 0xC6, 0xD6, 0xDE, 0x7C, 0x06, 0x07},        // Char 081 (Q)
        {0xFC, 0xC6, 0xC6, 0xFC, 0xD8, 0xCC, 0xC6, 0x00, 0x07},        // Char 082 (R)
        {0x7C, 0xC6, 0xC0, 0x7C, 0x06, 0xC6, 0x7C, 0x00, 0x07},        // Char 083 (S)
        {0xFF, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x08},        // Char 084 (T)
        {0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xFE, 0x00, 0x07},        // Char 085 (U)
        {0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x38, 0x00, 0x07},        // Char 086 (V)
        {0xC6, 0xC6, 0xC6, 0xC6, 0xD6, 0xFE, 0x6C, 0x00, 0x07},        // Char 087 (W)
        {0xC6, 0xC6, 0x6C, 0x38, 0x6C, 0xC6, 0xC6, 0x00, 0x07},        // Char 088 (X)
        {0xC6, 0xC6, 0xC6, 0x7C, 0x18, 0x30, 0xE0, 0x00, 0x07},        // Char 089 (Y)
        {0xFE, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xFE, 0x00, 0x07},        // Char 090 (Z)
        {0xF0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xF0, 0x00, 0x04},        // Char 091 ([)
        {0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x02, 0x00, 0x07},        // Char 092 (\)
        {0xF0, 0x30, 0x30, 0x30, 0x30, 0x30, 0xF0, 0x00, 0x04},        // Char 093 (])
        {0x10, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x07},        // Char 094 (^)
        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x08},        // Char 095 (_)
        {0xC0, 0xC0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03},        // Char 096 (`)
        {0x00, 0x00, 0x7C, 0x06, 0x7E, 0xC6, 0x7E, 0x00, 0x07},        // Char 097 (a)
        {0xC0, 0xC0, 0xFC, 0xC6, 0xC6, 0xC6, 0xFC, 0x00, 0x07},        // Char 098 (b)
        {0x00, 0x00, 0x7C, 0xC6, 0xC0, 0xC6, 0x7C, 0x00, 0x07},        // Char 099 (c)
        {0x06, 0x06, 0x7E, 0xC6, 0xC6, 0xC6, 0x7E, 0x00, 0x07},        // Char 100 (d)
        {0x00, 0x00, 0x7C, 0xC6, 0xFE, 0xC0, 0x7C, 0x00, 0x07},        // Char 101 (e)
        {0x38, 0x6C, 0x60, 0xF0, 0x60, 0x60, 0xF0, 0x00, 0x06},        // Char 102 (f)
        {0x00, 0x00, 0x7E, 0xC6, 0xC6, 0x7E, 0x06, 0xFC, 0x07},        // Char 103 (g)
        {0xC0, 0xC0, 0xFC, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x07},        // Char 104 (h)
        {0x60, 0x00, 0xE0, 0x60, 0x60, 0x60, 0xF0, 0x00, 0x04},        // Char 105 (i)
        {0x06, 0x00, 0x06, 0x06, 0x06, 0x06, 0xC6, 0x7C, 0x07},        // Char 106 (j)
        {0xC0, 0xC0, 0xCC, 0xD8, 0xF8, 0xCC, 0xC6, 0x00, 0x07},        // Char 107 (k)
        {0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x60, 0x00, 0x03},        // Char 108 (l)
        {0x00, 0x00, 0xCC, 0xFE, 0xFE, 0xD6, 0xD6, 0x00, 0x07},        // Char 109 (m)
        {0x00, 0x00, 0xFC, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x07},        // Char 110 (n)
        {0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x07},        // Char 111 (o)
        {0x00, 0x00, 0xFC, 0xC6, 0xC6, 0xFC, 0xC0, 0xC0, 0x07},        // Char 112 (p)
        {0x00, 0x00, 0x7E, 0xC6, 0xC6, 0x7E, 0x06, 0x06, 0x07},        // Char 113 (q)
        {0x00, 0x00, 0xFC, 0xC6, 0xC0, 0xC0, 0xC0, 0x00, 0x07},        // Char 114 (r)
        {0x00, 0x00, 0x7E, 0xC0, 0x7C, 0x06, 0xFC, 0x00, 0x07},        // Char 115 (s)
        {0x30, 0x30, 0xFC, 0x30, 0x30, 0x30, 0x1C, 0x00, 0x06},        // Char 116 (t)
        {0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0x7E, 0x00, 0x07},        // Char 117 (u)
        {0x00, 0x00, 0xC6, 0xC6, 0xC6, 0x7C, 0x38, 0x00, 0x07},        // Char 118 (v)
        {0x00, 0x00, 0xC6, 0xC6, 0xD6, 0xFE, 0x6C, 0x00, 0x07},        // Char 119 (w)
        {0x00, 0x00, 0xC6, 0x6C, 0x38, 0x6C, 0xC6, 0x00, 0x07},        // Char 120 (x)
        {0x00, 0x00, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0xFC, 0x07},        // Char 121 (y)
        {0x00, 0x00, 0xFE, 0x0C, 0x38, 0x60, 0xFE, 0x00, 0x07},        // Char 122 (z)
        {0x1C, 0x30, 0x30, 0xE0, 0x30, 0x30, 0x1C, 0x00, 0x06},        // Char 123 ({)
        {0xC0, 0xC0, 0xC0, 0x00, 0xC0, 0xC0, 0xC0, 0x00, 0x02},        // Char 124 (|)
        {0xE0, 0x30, 0x30, 0x1C, 0x30, 0x30, 0xE0, 0x00, 0x06},        // Char 125 (})
        {0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07},        // Char 126 (~)
        {0x00, 0x10, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0x00, 0x07}         // Char 127 (.)
};

/**
 * \asserts ( framebuffer != NULL )
 * \asserts ( framebuffer->set_pixel != NULL )
 */
enum status_code font_08px_draw_character (
  struct Framebuffer *framebuffer       //< pointer to the framebuffer to draw the bitmap to
,               char  character         //< character to draw
,           uint32_t  pixel_value       //< value to set for the pixels
,           uint16_t  x0                //< x start position of character bitmap in framebuffer
,           uint16_t  y0                //< y start position of character bitmap in framebuffer
,            uint8_t  font_background   //< how to draw font background
,            uint8_t *char_width        //< [out] width of character drawn in pixel
){
        Assert( framebuffer != NULL );
        Assert( framebuffer->set_pixel != NULL );

        Framebuffer_Set_Pixel *set_pixel = framebuffer->set_pixel;

            uint16_t  char_idx;         // index of the character byte in the font
                bool  is_set_pixel;     // true if we want to set pixel_value for that pixel
        uint_fast8_t  scanline;         // a single x line of bits 
        uint_fast8_t  x, y;             // current x and y pixel position to draw

        // find out which entry of the font array to use
        char_idx = character - 0x20;
        if( ( char_idx < 0 ) || ( char_idx > FONT_08PX_CHAR_IDX_MAX ) ) {
                char_idx = 1; // print <SPACE> for unknown characters
        }

        *char_width = font_08px[char_idx][FONT_08PX_CHAR_WIDTH_BYTE_IDX];

        // we paint forward in y- and in x-direction
        // - y is the scanline index for the character bitmap (we got from top to bottom)
        // - for the pixel information we use the scanline byte like a shift register and evaluate the 2^7 bit
        y = 0;        // y to start with
        do {
                x = 0;        // x to start with
                scanline = font_08px[char_idx][y];
                while( x < *char_width ){
                        is_set_pixel = scanline & 0x80; // evaluate the bit at 2^7
                        if( is_set_pixel ){
                                set_pixel( framebuffer, x0+x, y0+y, pixel_value );  // always draw pixel if to set
                        } else {
                                if( font_background == FONT_BACKGROUND_OPAQUE ){
                                        set_pixel( framebuffer, x0+x, y0+y, 0x00 ); // only clear background pixel, if mode is opaque
                                }
                        }
                        scanline = scanline << 1;
                        x++;
                }
                y++;
        } while ( y < FONT_08PX_HEIGHT );

        return STATUS_OK;
}

/**
 * \asserts ( framebuffer != NULL )
 * \asserts ( string      != NULL )
 */
enum status_code font_08px_draw_string (
  struct Framebuffer *framebuffer       //< pointer to the framebuffer to draw the bitmap to
,         const char *string            //< string to draw
,           uint32_t  pixel_value       //< value to set for the pixels
,           uint16_t  x0                //< x start position of character bitmap in framebuffer
,           uint16_t  y0                //< y start position of character bitmap in framebuffer
,            uint8_t  font_background   //< how to draw font background
,            uint8_t *string_width      //< [out] width of character drawn in pixel
){
        Assert( framebuffer != NULL );
        Assert( string      != NULL );

        *string_width = 0;

        uint_fast8_t  x_pos      = 0;
             uint8_t  char_width = 0;
                char  curr;

        for( uint_fast8_t idx = 0; idx < 256; idx++ ) {
                curr = string[idx];
                if( curr == 0x00 ){
                        break;
                }
                font_08px_draw_character( framebuffer, string[idx], pixel_value, x0+x_pos, y0, FONT_BACKGROUND_OPAQUE, &char_width);
                x_pos += char_width + 1;
                *string_width += char_width + 1;
        }
        if( *string_width > 0 ){
                (*string_width)--;
        }

        return STATUS_OK;
}