Prep for v1.2.0
[hackover2013-badge-firmware.git] / drivers / displays / tft / fonts.c
1 /**************************************************************************/
2 /*!
3 @file aafonts.c
4 @author K. Townsend (microBuilder.eu)
5
6 drawString based on an example from Eran Duchan:
7 http://www.pavius.net/downloads/tools/53-the-dot-factory
8
9 @section LICENSE
10
11 Software License Agreement (BSD License)
12
13 Copyright (c) 2012, microBuilder SARL
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 3. Neither the name of the copyright holders nor the
24 names of its contributors may be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
28 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /**************************************************************************/
39 #include "fonts.h"
40 #include "lcd.h"
41 #include "drawing.h"
42
43 /**************************************************************************/
44 /* */
45 /* ----------------------- Private Methods ------------------------------ */
46 /* */
47 /**************************************************************************/
48
49 /**************************************************************************/
50 /*!
51 @brief Draws a single bitmap character
52 */
53 /**************************************************************************/
54 void fontsDrawCharBitmap(const uint16_t xPixel, const uint16_t yPixel, uint16_t color, const char *glyph, uint8_t cols, uint8_t rows)
55 {
56 uint16_t currentY, currentX, indexIntoGlyph;
57 uint16_t _row, _col, _colPages;
58
59 // set initial current y
60 currentY = yPixel;
61 currentX = xPixel;
62
63 // Figure out how many columns worth of data we have
64 if (cols % 8)
65 _colPages = cols / 8 + 1;
66 else
67 _colPages = cols / 8;
68
69 for (_row = 0; _row < rows; _row++)
70 {
71 for (_col = 0; _col < _colPages; _col++)
72 {
73 if (_row == 0)
74 indexIntoGlyph = _col;
75 else
76 indexIntoGlyph = (_row * _colPages) + _col;
77
78 currentY = yPixel + _row;
79 currentX = xPixel + (_col*8);
80 // send the data byte
81 if (glyph[indexIntoGlyph] & (0X80)) drawPixel(currentX, currentY, color);
82 if (glyph[indexIntoGlyph] & (0X40)) drawPixel(currentX+1, currentY, color);
83 if (glyph[indexIntoGlyph] & (0X20)) drawPixel(currentX+2, currentY, color);
84 if (glyph[indexIntoGlyph] & (0X10)) drawPixel(currentX+3, currentY, color);
85 if (glyph[indexIntoGlyph] & (0X08)) drawPixel(currentX+4, currentY, color);
86 if (glyph[indexIntoGlyph] & (0X04)) drawPixel(currentX+5, currentY, color);
87 if (glyph[indexIntoGlyph] & (0X02)) drawPixel(currentX+6, currentY, color);
88 if (glyph[indexIntoGlyph] & (0X01)) drawPixel(currentX+7, currentY, color);
89 }
90 }
91 }
92
93 /**************************************************************************/
94 /* */
95 /* ----------------------- Public Methods ------------------------------- */
96 /* */
97 /**************************************************************************/
98
99 /**************************************************************************/
100 /*!
101 @brief Draws a string using the supplied font
102
103 @param[in] x
104 Starting x co-ordinate
105 @param[in] y
106 Starting y co-ordinate
107 @param[in] color
108 Color to use when rendering the font
109 @param[in] fontInfo
110 Pointer to the FONT_INFO to use when drawing the string
111 @param[in] str
112 The string to render
113
114 @section Example
115
116 @code
117
118 #include "drivers/displays/tft/fonts/dejavusans9.h"
119
120 fontsDrawString(0, 90, COLOR_BLACK, &dejaVuSans9ptFontInfo, "DejaVu Sans 9");
121 fontsDrawString(0, 105, COLOR_BLACK, &dejaVuSans9ptFontInfo, "123456789012345678901234567890");
122
123 @endcode
124 */
125 /**************************************************************************/
126 void fontsDrawString(uint16_t x, uint16_t y, uint16_t color, const FONT_INFO *fontInfo, char *str)
127 {
128 uint16_t currentX, charWidth, characterToOutput;
129 const FONT_CHAR_INFO *charInfo;
130 uint16_t charOffset;
131
132 // set current x, y to that of requested
133 currentX = x;
134
135 // while not NULL
136 while (*str != '\0')
137 {
138 // get character to output
139 characterToOutput = *str;
140
141 // get char info
142 charInfo = fontInfo->charInfo;
143
144 // some fonts have character descriptors, some don't
145 if (charInfo != NULL)
146 {
147 // get correct char offset
148 charInfo += (characterToOutput - fontInfo->startChar);
149
150 // get width from char info
151 charWidth = charInfo->widthBits;
152
153 // get offset from char info
154 charOffset = charInfo->offset;
155 }
156 else
157 {
158 // if no char info, char width is always 5
159 charWidth = 5;
160
161 // char offset - assume 5 * letter offset
162 charOffset = (characterToOutput - fontInfo->startChar) * 5;
163 }
164
165 // Send individual characters
166 // We need to manually calculate width in pages since this is screwy with variable width fonts
167 //uint8_t heightPages = charWidth % 8 ? charWidth / 8 : charWidth / 8 + 1;
168 fontsDrawCharBitmap(currentX, y, color, (const char *)(&fontInfo->data[charOffset]), charWidth, fontInfo->height);
169
170 // next char X
171 currentX += charWidth + 1;
172
173 // next char
174 str++;
175 }
176 }
177
178 /**************************************************************************/
179 /*!
180 @brief Returns the width in pixels of a string when it is rendered
181
182 This method can be used to determine whether a string will fit
183 inside a specific area, or if it needs to be broken up into multiple
184 lines to be properly rendered on the screen.
185
186 This function only applied to bitmap fonts (which can have variable
187 widths). All smallfonts (if available) are fixed width and can
188 easily have their width calculated without costly functions like
189 this one.
190
191 @param[in] fontInfo
192 Pointer to the FONT_INFO for the font that will be used
193 @param[in] str
194 The string that will be rendered
195 */
196 /**************************************************************************/
197 uint16_t fontsGetStringWidth(const FONT_INFO *fontInfo, char *str)
198 {
199 uint16_t width = 0;
200 uint32_t currChar;
201 uint32_t startChar = fontInfo->startChar;
202
203 // until termination
204 for (currChar = *str; currChar; currChar = *(++str))
205 {
206 // if char info exists for the font, use width from there
207 if (fontInfo->charInfo != NULL)
208 {
209 width += fontInfo->charInfo[currChar - startChar].widthBits + 1;
210 }
211 else
212 {
213 width += 5 + 1;
214 }
215 }
216
217 /* return the width */
218 return width > 0 ? width - 1 : width;
219 }
This page took 0.065275 seconds and 5 git commands to generate.