COSA
An Object-Oriented Platform for Arduino Programming
Font.cpp
Go to the documentation of this file.
1 
21 #include "Font.hh"
22 
23 void
24 Font::draw(Canvas* canvas, char c, uint16_t x, uint16_t y,
25  uint8_t scale)
26 {
27  Glyph glyph(this, c);
28 
29  if (scale == 1) {
30  for (uint16_t i = 0; i < HEIGHT; i += 8) {
31  for (uint16_t j = 0; j < WIDTH; j++) {
32  uint8_t bits = glyph.next();
33  if (bits == 0xff) {
34  canvas->draw_vertical_line(x + j, y + i, CHARBITS);
35  }
36  else {
37  for (uint8_t k = 0; k < 8; k++) {
38  if (bits == 0) break;
39  if (bits & 1) canvas->draw_pixel(x + j, y + k + i);
40  bits >>= 1;
41  }
42  }
43  }
44  }
45  }
46  else {
47  for (uint16_t i = 0; i < HEIGHT; i += 8) {
48  for (uint16_t j = 0; j < WIDTH; j++) {
49  uint8_t bits = glyph.next();
50  for (uint8_t k = 0; k < 8; k++) {
51  if (bits == 0) break;
52  if (bits & 1) canvas->fill_rect(x + j*scale, y + (k+i)*scale, scale, scale);
53  bits >>= 1;
54  }
55  }
56  }
57  }
58 }
59 
60 #define ESCAPED_BITSET 0x1
61 
62 void
64 {
65  unsigned char chr = c;
66 
67  if (chr < m_font->FIRST || chr > m_font->LAST) {
68  m_bitmap = NULL;
69  return;
70  }
71 
72  m_offset = 0;
73  m_flags = 0;
74 
75  uint8_t uncompressed_size = m_font->WIDTH * ((m_font->HEIGHT + (CHARBITS-1)) / CHARBITS);
76 
77  switch (m_font->m_compression_type)
78  {
79  case 0: // uncompressed
80  m_bitmap = (uint8_t*)&m_font->m_bitmap[(chr - m_font->FIRST)*uncompressed_size];
81  break;
82 
83  case 1: // non-zero "present" bitset
84  uint16_t bitset_offset;
85  uint8_t bitset_size;
86 
87  bitset_offset = pgm_read_byte(&m_font->m_bitmap[(chr - m_font->FIRST)*2]) << 8;
88  bitset_offset |= pgm_read_byte(&m_font->m_bitmap[(chr - m_font->FIRST)*2 + 1]);
89  // indicates an escaped bitset (twice as wide)
90  if (bitset_offset & 0x8000) {
91  m_flags |= ESCAPED_BITSET;
92  bitset_offset &= 0x7FFF;
93  }
94  m_bitset = (uint8_t*)&m_font->m_bitmap[bitset_offset];
95 
96  bitset_size = (uncompressed_size + (CHARBITS-1)) / CHARBITS;
97  if (m_flags & ESCAPED_BITSET)
98  bitset_size *=2 ;
99 
100  m_bitmap = m_bitset + bitset_size;
101  m_next = 0;
102  break;
103 
104  default:
105  m_bitmap = NULL;
106  }
107 }
108 
109 uint8_t
111 {
112  // unrecognized compression type or
113  // character not available in font shows streaks
114  if (!m_bitmap)
115  return (0x55);
116 
117  uint8_t result = 0;
118 
119  switch (m_font->m_compression_type)
120  {
121  case 0: // uncompressed
122  result = pgm_read_byte(&m_bitmap[m_offset++]);
123  break;
124 
125  case 1: // non-zero "present" bitset
126  uint8_t bitset_offset = m_offset >> 3;
127  if (m_flags & ESCAPED_BITSET)
128  bitset_offset = (bitset_offset*2)+1;
129  uint8_t bitset_bit = 1 << (7 - (m_offset % 8));
130 
131  m_offset++;
132 
133  // If byte is present
134  if (pgm_read_byte(&m_bitset[bitset_offset]) & bitset_bit)
135  result = pgm_read_byte(&m_bitmap[m_next++]);
136  break;
137  }
138 
139  return(result);
140 }
#define ESCAPED_BITSET
Definition: Font.cpp:60
const uint8_t * m_bitmap
Definition: Font.hh:143
#define NULL
Definition: Types.h:101
uint8_t next()
Definition: Font.cpp:110
const uint8_t WIDTH
Definition: Font.hh:35
const uint8_t HEIGHT
Definition: Font.hh:36
virtual void fill_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Definition: Canvas.cpp:151
#define CHARBITS
Definition: Types.h:57
const uint8_t FIRST
Definition: Font.hh:43
virtual void draw_vertical_line(uint16_t x, uint16_t y, uint16_t length)
Definition: Canvas.cpp:198
virtual void draw(Canvas *canvas, char c, uint16_t x, uint16_t y, uint8_t scale)
Definition: Font.cpp:24
virtual void draw_pixel(uint16_t x, uint16_t y)
Definition: Canvas.cpp:63
Definition: Canvas.hh:44
void begin(char c)
Definition: Font.cpp:63