Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | // font class library |
2 | #pragma once |
||
4 | mjames | 3 | #include <cstdint> |
2 | mjames | 4 | |
5 | class font_t |
||
6 | { |
||
7 | public: |
||
17 | mjames | 8 | |
9 | |||
4 | mjames | 10 | /// \param height - pixel character height |
11 | /// \param width - pixel character width |
||
12 | /// \param spacing - character to character spacing |
||
13 | /// \param chars - number of characters in character set |
||
14 | /// \param data - constant data |
||
17 | mjames | 15 | /// \param mult - coordinate multiplied by before sampling bitmap |
16 | /// \param shift- coordinate right shifted by after multiplication : mult=1, shift=1 means half sized |
||
13 | mjames | 17 | font_t(uint8_t const height, uint8_t const width, |
18 | uint8_t const spacing, uint8_t const chars, |
||
17 | mjames | 19 | char const *data, |
20 | uint8_t const mult, uint8_t const shift) |
||
21 | : m_height(height), |
||
22 | m_width(width), |
||
23 | m_mapheight(PIXELS(height,mult,shift)), |
||
24 | m_mapwidth(PIXELS(width,mult,shift)), |
||
25 | m_spacing(PIXELS(spacing,mult,shift)), |
||
26 | m_chars(chars), |
||
27 | m_data(data), |
||
28 | m_mult(mult), |
||
29 | m_shift(shift), |
||
30 | m_round(ROUNDING(shift)) |
||
2 | mjames | 31 | { |
32 | } |
||
33 | |||
34 | virtual char |
||
13 | mjames | 35 | getPixel(char c, int x, int y) = 0; |
2 | mjames | 36 | |
37 | // character width |
||
4 | mjames | 38 | uint8_t |
13 | mjames | 39 | width() |
2 | mjames | 40 | { |
17 | mjames | 41 | return m_mapwidth; |
2 | mjames | 42 | } |
43 | |||
44 | // character height |
||
4 | mjames | 45 | uint8_t |
13 | mjames | 46 | height() |
2 | mjames | 47 | { |
17 | mjames | 48 | return m_mapheight; |
2 | mjames | 49 | } |
50 | |||
4 | mjames | 51 | uint8_t |
13 | mjames | 52 | chars() |
4 | mjames | 53 | { |
54 | return m_chars; |
||
55 | } |
||
56 | |||
57 | uint8_t |
||
13 | mjames | 58 | spacing() |
4 | mjames | 59 | { |
60 | return m_spacing; |
||
61 | } |
||
13 | mjames | 62 | |
17 | mjames | 63 | private: |
64 | |||
65 | /// @brief Convert from real bitmap dimension to virtual dimension given |
||
66 | /// @param dimension original dimension |
||
67 | /// @param mult scale up multiplier |
||
68 | /// @param shift bit shift right after scaling |
||
69 | /// @return virtual dimension |
||
70 | static constexpr unsigned PIXELS(uint8_t dimension,uint8_t mult, uint8_t shift) { return (dimension << shift )/mult; }; |
||
71 | |||
72 | /// @brief Calculate a rounding value that is 0.5 << shift |
||
73 | /// @param val shift value |
||
74 | /// @return 0.5 << shift or 0 |
||
75 | static constexpr uint8_t ROUNDING(uint8_t shift) { return (shift>0) ? (0) : 1<<(shift-1);}; |
||
76 | |||
77 | |||
2 | mjames | 78 | protected: |
13 | mjames | 79 | /// @brief Pixel height |
4 | mjames | 80 | uint8_t const m_height; |
13 | mjames | 81 | /// @brief Bitmap width |
4 | mjames | 82 | uint8_t const m_width; |
17 | mjames | 83 | |
84 | /// @brief bitmap virtual height |
||
85 | uint8_t const m_mapheight; |
||
86 | /// @brief Bitmap virtual width |
||
87 | uint8_t const m_mapwidth; |
||
88 | |||
13 | mjames | 89 | /// @brief Spacing between characters |
4 | mjames | 90 | uint8_t const m_spacing; |
13 | mjames | 91 | /// @brief Number of characters in the character set bit map |
17 | mjames | 92 | |
93 | |||
4 | mjames | 94 | uint8_t const m_chars; |
2 | mjames | 95 | char const *m_data; |
17 | mjames | 96 | |
97 | unsigned char const m_mult; // |
||
98 | unsigned char const m_shift; // bit shift after multiplication |
||
99 | unsigned char const m_round; // round up value 0.5 << shift |
||
2 | mjames | 100 | }; |
101 | |||
102 | class font5x7_t : public font_t |
||
103 | { |
||
104 | public: |
||
13 | mjames | 105 | // one byte of pixels per character row. |
17 | mjames | 106 | font5x7_t(unsigned char_count, char const *data) : font_t(7, 5, 6, char_count, data, 1, 0){}; |
2 | mjames | 107 | |
4 | mjames | 108 | char |
13 | mjames | 109 | getPixel(char c, int x, int y) override; |
2 | mjames | 110 | }; |
111 | |||
112 | class font10x18_t : public font_t |
||
113 | { |
||
114 | public: |
||
17 | mjames | 115 | |
116 | // XBM format with optional multiply, right shift scaling of coordinates |
||
117 | font10x18_t(unsigned char_count, char const *data, unsigned char mult=1, unsigned char shift=0) : font_t(18,10,10, char_count, data, mult, shift){}; |
||
2 | mjames | 118 | |
119 | char |
||
13 | mjames | 120 | getPixel(char c, int x, int y) override; |
2 | mjames | 121 | }; |
122 | |||
123 | // defined fonts |
||
124 | // original 5x7 |
||
125 | extern font5x7_t small_font; |
||
126 | |||
18 | mjames | 127 | |
2 | mjames | 128 | // lucida font |
129 | // in 10x18 |
||
130 | extern font10x18_t large_font; |
||
17 | mjames | 131 | |
18 | mjames | 132 | // 4/5 scaled down lucida font |
133 | // |
||
134 | extern font10x18_t medium_font; |
||
135 | |||
136 | /// half sized lucida font |
||
137 | extern font10x18_t half_font; |