API changes for v1.0.0
[hackover2013-badge-firmware.git] / drivers / displays / tft / aafonts.c
1 /**************************************************************************/
2 /*!
3 @file aafonts.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2012, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include "aafonts.h"
37
38 #include "drivers/displays/tft/lcd.h"
39 #include "drivers/displays/tft/drawing.h"
40
41 /**************************************************************************/
42 /* */
43 /* ----------------------- Private Methods ------------------------------ */
44 /* */
45 /**************************************************************************/
46
47 /**************************************************************************/
48 /*!
49 @brief Renders a single AA2 character on the screen
50
51 This text rendering method used a lookup table of pre-calculated
52 colors, and doesn't require any reads from the LCD (not all displays
53 support reading pixels back). This offers the best performance and
54 high-quality text, but can only be used on solid backgrounds where
55 the bgcolor is known.
56
57 @param[in] x
58 Top-left x position
59 @param[in] y
60 Top-left y position
61 @param[in] height
62 Font height in pixels
63 @param[in] character
64 Pointer to the aafontsCharInfo_t array with the char data
65 @param[in] colorTable
66 Pointer to the 4 element color lookup table
67 */
68 /**************************************************************************/
69 int aafontsDrawCharAA2( uint16_t x, uint16_t y, uint16_t height, aafontsCharInfo_t character, const uint16_t * colorTable)
70 {
71 uint16_t w, h, xp, yp, pos;
72 uint8_t color;
73
74 for (h = 0; h < height; h++)
75 {
76 pos = 0;
77 for (w = 0; w < character.width; w++)
78 {
79 color = character.charData[h*character.bytesPerRow + w/4];
80 switch (pos)
81 {
82 case 0:
83 color = (color >> 6) & 0x03;
84 break;
85 case 1:
86 color = (color >> 4) & 0x03;
87 break;
88 case 2:
89 color = (color >> 2) & 0x03;
90 break;
91 case 3:
92 color = color & 0x03;
93 break;
94 }
95 if (color) lcdDrawPixel(x+w, y+h, colorTable[color & 0xF]);
96 pos++;
97 if (pos == 4) pos = 0;
98 }
99 }
100 }
101
102 /**************************************************************************/
103 /*!
104 @brief Renders a single AA4 character on the screen
105
106 This text rendering method used a lookup table of pre-calculated
107 colors, and doesn't require any reads from the LCD (not all displays
108 support reading pixels back). This offers the best performance and
109 high-quality text, but can only be used on solid backgrounds where
110 the bgcolor is known.
111
112 @param[in] x
113 Top-left x position
114 @param[in] y
115 Top-left y position
116 @param[in] height
117 Font height in pixels
118 @param[in] character
119 Pointer to the aafontsCharInfo_t array with the char data
120 @param[in] colorTable
121 Pointer to the 16 element color lookup table
122 */
123 /**************************************************************************/
124 int aafontsDrawCharAA4( uint16_t x, uint16_t y, uint16_t height, aafontsCharInfo_t character, const uint16_t * colorTable)
125 {
126 uint16_t w, h, xp, yp;
127 uint8_t color;
128
129 for (h = 0; h < height; h++)
130 {
131 for (w = 0; w < character.width; w++)
132 {
133 color = character.charData[h*character.bytesPerRow + w/2];
134 if (!(w % 2)) color = (color >> 4);
135 if (color) lcdDrawPixel(x+w, y+h, colorTable[color & 0xF]);
136 }
137 }
138 }
139
140 /**************************************************************************/
141 /* */
142 /* ----------------------- Public Methods ------------------------------- */
143 /* */
144 /**************************************************************************/
145
146 /**************************************************************************/
147 /*!
148 @brief Draws a string using the supplied anti-aliased font
149
150 @param[in] x
151 Starting x co-ordinate
152 @param[in] y
153 Starting y co-ordinate
154 @param[in] colorTable
155 The color lookup table to use for the antialiased pixels
156 @param[in] font
157 Pointer to the aafontsFont_t to use when drawing the string
158 @param[in] str
159 The string to render
160
161 @section Example
162
163 @code
164
165 #include "drivers/displays/tft/aafonts.h"
166 #include "drivers/displays/tft/aafonts/aa2/DejaVuSansCondensed14_AA2.h"
167 #include "drivers/displays/tft/aafonts/aa2/DejaVuSansCondensedBold14_AA2.h"
168
169 // Fill screen with white (so that we can use the pre-defined color tables in aafonts.h)
170 lcdFillRGB(COLOR_WHITE);
171
172 aafontsDrawString(10, 100, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "1234567890");
173 aafontsDrawString(10, 120, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
174 aafontsDrawString(10, 140, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "abcdefghijklmnopqrstuvwxyz");
175 aafontsDrawString(10, 160, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "!\"#$%&'()*+,-./ :;<=>?");
176 aafontsDrawString(10, 180, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "@ [\\]^_ {|}~");
177
178 aafontsDrawString(10, 215, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "1234567890");
179 aafontsDrawString(10, 235, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
180 aafontsDrawString(10, 255, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "abcdefghijklmnopqrstuvwxyz");
181 aafontsDrawString(10, 275, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "!\"#$%&'()*+,-./ :;<=>?");
182 aafontsDrawString(10, 295, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "@ [\\]^_ {|}~");
183
184 @endcode
185 */
186 /**************************************************************************/
187 void aafontsDrawString(uint16_t x, uint16_t y, const uint16_t * colorTable, const aafontsFont_t *font, char *str)
188 {
189 uint16_t currentX, charWidth, characterToOutput;
190 const aafontsCharInfo_t *charInfo;
191 uint16_t charOffset;
192
193 // set current x, y to that of requested
194 currentX = x;
195
196 // while not NULL
197 while (*str != '\0')
198 {
199 // get character to output
200 characterToOutput = *str;
201 uint16_t last = font->lastChar;
202
203 // Check if the character is within the font boundaries
204 if ((characterToOutput > font->lastChar) || (characterToOutput < font->firstChar))
205 {
206 // Character is out of bounds
207 // Insert space instead
208 charWidth = font->unknownCharWidth;
209 }
210 else
211 {
212 // get char info
213 charInfo = &(font->charTable[characterToOutput - font->firstChar]);
214 // get width from char info
215 charWidth = charInfo->width;
216 // Send individual characters
217 switch (font->fontType)
218 {
219 case AAFONTS_FONTTYPE_AA2:
220 aafontsDrawCharAA2(currentX, y, font->fontHeight, *charInfo, &colorTable[0]);
221 break;
222 case AAFONTS_FONTTYPE_AA4:
223 aafontsDrawCharAA4(currentX, y, font->fontHeight, *charInfo, &colorTable[0]);
224 break;
225 }
226 }
227
228 // Adjust x for the next character
229 currentX += charWidth;
230
231 // next char in string
232 str++;
233 }
234 }
235
236 /**************************************************************************/
237 /*!
238 @brief Returns the width in pixels of a string when it is rendered
239
240 This method can be used to determine whether a string will fit
241 inside a specific area, or if it needs to be broken up into multiple
242 lines to be properly rendered on the screen.
243
244 @param[in] font
245 Pointer to aafontsFont_t of the font that will be used
246 @param[in] str
247 The string that will be rendered
248
249 @section Example
250
251 @code
252
253 #include "drivers/displays/tft/aafonts.h"
254 #include "drivers/displays/tft/aafonts/aa2/DejaVuSansCondensed14_AA2.h"
255
256 uint32_t w = aafontsGetStringWidth(&DejaVuSansCondensed14_AA2, "This is a simple test 123!!! (AA2)");
257
258 @endcode
259 */
260 /**************************************************************************/
261 uint16_t aafontsGetStringWidth(const aafontsFont_t *font, char *str)
262 {
263 uint16_t width = 0;
264 const aafontsCharInfo_t *charInfo;
265 uint32_t currChar;
266 uint32_t startChar = font->firstChar;
267
268 // until termination
269 for (currChar = *str; currChar; currChar = *(++str))
270 {
271 // Check if the character is within the font boundaries
272 if ((currChar > font->lastChar) || (currChar < font->firstChar))
273 {
274 // Character is out of bounds
275 width += font->unknownCharWidth;
276 }
277 else
278 {
279 // get char info
280 charInfo = &(font->charTable[currChar - font->firstChar]);
281 // get width from char info
282 width += charInfo->width;
283 }
284 }
285
286 /* return the string width */
287 return width;
288 }
This page took 0.064983 seconds and 5 git commands to generate.