FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef FONT_HH
22#define FONT_HH
23
24#include <avr/pgmspace.h>
25
26namespace devices::display
27{
38 template<bool VERTICAL_> class Font
39 {
40 public:
42 static constexpr bool VERTICAL = VERTICAL_;
43
55 constexpr Font(char first_char, char last_char, uint8_t width, uint8_t height, const uint8_t* glyphs)
56 : first_char_{uint8_t(first_char)}, last_char_{uint8_t(last_char)},
57 width_{width}, height_{height},
58 glyph_rows_{uint8_t(VERTICAL ? (height - 1) / 8 + 1 : height)},
59 glyph_cols_{uint8_t(VERTICAL ? width : (width - 1) / 8 + 1)},
60 glyph_size_{uint8_t(glyph_rows_ * glyph_cols_)},
61 glyphs_{glyphs} {}
62
64 uint8_t width() const
65 {
66 return width_;
67 }
68
70 uint8_t height() const
71 {
72 return height_;
73 }
74
76 uint8_t first_char() const
77 {
78 return first_char_;
79 }
80
82 uint8_t last_char() const
83 {
84 return last_char_;
85 }
86
99 uint8_t glyph_rows() const
100 {
101 return glyph_rows_;
102 }
103
117 uint8_t glyph_cols() const
118 {
119 return glyph_cols_;
120 }
121
123 uint8_t glyph_size() const
124 {
125 return glyph_size_;
126 }
127
139 uint16_t get_char_glyph_ref(char value) const
140 {
141 const uint8_t val = uint8_t(value);
142 if ((val < first_char_) || (val > last_char_))
143 return 0;
144 // Find first byte of character in glyphs_
145 uint16_t index = (val - first_char_) * glyph_size();
146 return uint16_t(&glyphs_[index]);
147 }
148
161 //TODO rather have row and col as args no?
162 uint8_t get_char_glyph_byte(uint16_t glyph_ref, uint8_t index) const
163 {
164 if (index >= glyph_size()) return 0;
165 return pgm_read_byte(glyph_ref + index);
166 }
167
168 private:
169 // Supported character codes
170 const uint8_t first_char_;
171 const uint8_t last_char_;
172
173 // Font size
174 const uint8_t width_;
175 const uint8_t height_;
176 // Glyph size in bytes: how many bytes for a row, how many bytes for a column
177 const uint8_t glyph_rows_;
178 const uint8_t glyph_cols_;
179 const uint8_t glyph_size_;
180
181 // Font used in characters display
182 // const uint8_t glyph_size_;
183 const uint8_t* glyphs_ = nullptr;
184 };
185}
186
187#endif /* FONT_HH */
Generic font support class.
Definition: font.h:39
constexpr Font(char first_char, char last_char, uint8_t width, uint8_t height, const uint8_t *glyphs)
Construct a new Font.
Definition: font.h:55
static constexpr bool VERTICAL
true if font is vertical, false if horizontal
Definition: font.h:42
uint8_t get_char_glyph_byte(uint16_t glyph_ref, uint8_t index) const
Get one byte of character glyph.
Definition: font.h:162
uint8_t last_char() const
Code of last char supported by this Font.
Definition: font.h:82
uint8_t glyph_rows() const
Determine the number of rows this font uses for each of its glyphs.
Definition: font.h:99
uint16_t get_char_glyph_ref(char value) const
Get a glyph reference for the requested character value.
Definition: font.h:139
uint8_t glyph_cols() const
Determine the number of columns this font uses for each of its glyphs.
Definition: font.h:117
uint8_t first_char() const
Code of first char supported by this Font.
Definition: font.h:76
uint8_t glyph_size() const
Glyph size in bytes.
Definition: font.h:123
uint8_t width() const
Width of font glyphs in pixels.
Definition: font.h:64
uint8_t height() const
Height of font glyphs in pixels.
Definition: font.h:70
Defines generic API for all display devices.
Definition: display.h:71