1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Software License Agreement (BSD License)
10 Copyright (c) 2012, microBuilder SARL
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.
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.
35 /**************************************************************************/
38 #include "drivers/displays/tft/lcd.h"
39 #include "drivers/displays/tft/drawing.h"
41 // Common color lookup tables for AA2 (4-color anti-aliased) fonts
42 static const uint16_t COLORTABLE_AA2_WHITEONBLACK
[4] = { 0x0000, 0x52AA, 0xAD55, 0xFFFF};
43 static const uint16_t COLORTABLE_AA2_BLACKONWHITE
[4] = { 0xFFFF, 0xAD55, 0x52AA, 0x0000};
45 // Common color lookup tables for AA4 (16-color anti-aliased) fonts
46 static const uint16_t COLORTABLE_AA4_WHITEONBLACK
[16] = { 0x0000, 0x1082, 0x2104, 0x3186, 0x4208, 0x528A, 0x630C, 0x738E, 0x8410, 0x9492, 0xA514, 0xB596, 0xC618, 0xD69A, 0xE71C, 0xFFFF};
47 static const uint16_t COLORTABLE_AA4_BLACKONWHITE
[16] = { 0xFFFF, 0xE71C, 0xD69A, 0xC618, 0xB596, 0xA514, 0x9492, 0x8410, 0x738E, 0x630C, 0x528A, 0x4208, 0x3186, 0x2104, 0x1082, 0x0000};
49 /**************************************************************************/
51 /* ----------------------- Private Methods ------------------------------ */
53 /**************************************************************************/
55 /**************************************************************************/
57 @brief Renders a single AA2 character on the screen
59 This text rendering method used a lookup table of pre-calculated
60 colors, and doesn't require any reads from the LCD (not all displays
61 support reading pixels back). This offers the best performance and
62 high-quality text, but can only be used on solid backgrounds where
72 Pointer to the aafontsCharInfo_t array with the char data
74 Pointer to the 4 element color lookup table
76 /**************************************************************************/
77 void aafontsDrawCharAA2( uint16_t x
, uint16_t y
, uint16_t height
, aafontsCharInfo_t character
, const uint16_t * colorTable
)
82 for (h
= 0; h
< height
; h
++)
85 for (w
= 0; w
< character
.width
; w
++)
87 color
= character
.charData
[h
*character
.bytesPerRow
+ w
/4];
91 color
= (color
>> 6) & 0x03;
94 color
= (color
>> 4) & 0x03;
97 color
= (color
>> 2) & 0x03;
100 color
= color
& 0x03;
103 if (color
) lcdDrawPixel(x
+w
, y
+h
, colorTable
[color
& 0xF]);
105 if (pos
== 4) pos
= 0;
110 /**************************************************************************/
112 @brief Renders a single AA4 character on the screen
114 This text rendering method used a lookup table of pre-calculated
115 colors, and doesn't require any reads from the LCD (not all displays
116 support reading pixels back). This offers the best performance and
117 high-quality text, but can only be used on solid backgrounds where
118 the bgcolor is known.
125 Font height in pixels
127 Pointer to the aafontsCharInfo_t array with the char data
128 @param[in] colorTable
129 Pointer to the 16 element color lookup table
131 /**************************************************************************/
132 void aafontsDrawCharAA4( uint16_t x
, uint16_t y
, uint16_t height
, aafontsCharInfo_t character
, const uint16_t * colorTable
)
137 for (h
= 0; h
< height
; h
++)
139 for (w
= 0; w
< character
.width
; w
++)
141 color
= character
.charData
[h
*character
.bytesPerRow
+ w
/2];
142 if (!(w
% 2)) color
= (color
>> 4);
143 if (color
) lcdDrawPixel(x
+w
, y
+h
, colorTable
[color
& 0xF]);
148 /**************************************************************************/
150 /* ----------------------- Public Methods ------------------------------- */
152 /**************************************************************************/
154 /**************************************************************************/
156 @brief Draws a string using the supplied anti-aliased font
159 Starting x co-ordinate
161 Starting y co-ordinate
162 @param[in] colorTable
163 The color lookup table to use for the antialiased pixels
165 Pointer to the aafontsFont_t to use when drawing the string
173 #include "drivers/displays/tft/aafonts.h"
174 #include "drivers/displays/tft/aafonts/aa2/DejaVuSansCondensed14_AA2.h"
175 #include "drivers/displays/tft/aafonts/aa2/DejaVuSansCondensedBold14_AA2.h"
177 // Fill screen with white (so that we can use the pre-defined color tables in aafonts.h)
178 lcdFillRGB(COLOR_WHITE);
180 aafontsDrawString(10, 100, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "1234567890");
181 aafontsDrawString(10, 120, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
182 aafontsDrawString(10, 140, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "abcdefghijklmnopqrstuvwxyz");
183 aafontsDrawString(10, 160, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "!\"#$%&'()*+,-./ :;<=>?");
184 aafontsDrawString(10, 180, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensed14_AA2, "@ [\\]^_ {|}~");
186 aafontsDrawString(10, 215, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "1234567890");
187 aafontsDrawString(10, 235, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
188 aafontsDrawString(10, 255, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "abcdefghijklmnopqrstuvwxyz");
189 aafontsDrawString(10, 275, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "!\"#$%&'()*+,-./ :;<=>?");
190 aafontsDrawString(10, 295, COLORTABLE_AA2_BLACKONWHITE, &DejaVuSansCondensedBold14_AA2, "@ [\\]^_ {|}~");
194 /**************************************************************************/
195 void aafontsDrawString(uint16_t x
, uint16_t y
, const uint16_t * colorTable
, const aafontsFont_t
*font
, char *str
)
197 uint16_t currentX
, charWidth
, characterToOutput
;
198 const aafontsCharInfo_t
*charInfo
;
200 // set current x, y to that of requested
206 // get character to output
207 characterToOutput
= *str
;
209 // Check if the character is within the font boundaries
210 if ((characterToOutput
> font
->lastChar
) || (characterToOutput
< font
->firstChar
))
212 // Character is out of bounds
213 // Insert space instead
214 charWidth
= font
->unknownCharWidth
;
219 charInfo
= &(font
->charTable
[characterToOutput
- font
->firstChar
]);
220 // get width from char info
221 charWidth
= charInfo
->width
;
222 // Send individual characters
223 switch (font
->fontType
)
225 case AAFONTS_FONTTYPE_AA2
:
226 aafontsDrawCharAA2(currentX
, y
, font
->fontHeight
, *charInfo
, &colorTable
[0]);
228 case AAFONTS_FONTTYPE_AA4
:
229 aafontsDrawCharAA4(currentX
, y
, font
->fontHeight
, *charInfo
, &colorTable
[0]);
234 // Adjust x for the next character
235 currentX
+= charWidth
;
237 // next char in string
242 /**************************************************************************/
244 @brief Returns the width in pixels of a string when it is rendered
246 This method can be used to determine whether a string will fit
247 inside a specific area, or if it needs to be broken up into multiple
248 lines to be properly rendered on the screen.
251 Pointer to aafontsFont_t of the font that will be used
253 The string that will be rendered
259 #include "drivers/displays/tft/aafonts.h"
260 #include "drivers/displays/tft/aafonts/aa2/DejaVuSansCondensed14_AA2.h"
262 uint32_t w = aafontsGetStringWidth(&DejaVuSansCondensed14_AA2, "This is a simple test 123!!! (AA2)");
266 /**************************************************************************/
267 uint16_t aafontsGetStringWidth(const aafontsFont_t
*font
, char *str
)
270 const aafontsCharInfo_t
*charInfo
;
274 for (currChar
= *str
; currChar
; currChar
= *(++str
))
276 // Check if the character is within the font boundaries
277 if ((currChar
> font
->lastChar
) || (currChar
< font
->firstChar
))
279 // Character is out of bounds
280 width
+= font
->unknownCharWidth
;
285 charInfo
= &(font
->charTable
[currChar
- font
->firstChar
]);
286 // get width from char info
287 width
+= charInfo
->width
;
291 /* return the string width */