1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 drawString based on an example from Eran Duchan:
7 http://www.pavius.net/downloads/tools/53-the-dot-factory
11 Software License Agreement (BSD License)
13 Copyright (c) 2012, microBuilder SARL
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.
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.
38 /**************************************************************************/
43 /**************************************************************************/
45 /* ----------------------- Private Methods ------------------------------ */
47 /**************************************************************************/
49 /**************************************************************************/
51 @brief Draws a single bitmap character
53 /**************************************************************************/
54 void fontsDrawCharBitmap(const uint16_t xPixel
, const uint16_t yPixel
, uint16_t color
, const char *glyph
, uint8_t cols
, uint8_t rows
)
56 uint16_t currentY
, currentX
, indexIntoGlyph
;
57 uint16_t _row
, _col
, _colPages
;
59 // set initial current y
63 // Figure out how many columns worth of data we have
65 _colPages
= cols
/ 8 + 1;
69 for (_row
= 0; _row
< rows
; _row
++)
71 for (_col
= 0; _col
< _colPages
; _col
++)
74 indexIntoGlyph
= _col
;
76 indexIntoGlyph
= (_row
* _colPages
) + _col
;
78 currentY
= yPixel
+ _row
;
79 currentX
= xPixel
+ (_col
*8);
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
);
93 /**************************************************************************/
95 /* ----------------------- Public Methods ------------------------------- */
97 /**************************************************************************/
99 /**************************************************************************/
101 @brief Draws a string using the supplied font
104 Starting x co-ordinate
106 Starting y co-ordinate
108 Color to use when rendering the font
110 Pointer to the FONT_INFO to use when drawing the string
118 #include "drivers/displays/tft/fonts/dejavusans9.h"
120 fontsDrawString(0, 90, COLOR_BLACK, &dejaVuSans9ptFontInfo, "DejaVu Sans 9");
121 fontsDrawString(0, 105, COLOR_BLACK, &dejaVuSans9ptFontInfo, "123456789012345678901234567890");
125 /**************************************************************************/
126 void fontsDrawString(uint16_t x
, uint16_t y
, uint16_t color
, const FONT_INFO
*fontInfo
, char *str
)
128 uint16_t currentX
, charWidth
, characterToOutput
;
129 const FONT_CHAR_INFO
*charInfo
;
132 // set current x, y to that of requested
138 // get character to output
139 characterToOutput
= *str
;
142 charInfo
= fontInfo
->charInfo
;
144 // some fonts have character descriptors, some don't
145 if (charInfo
!= NULL
)
147 // get correct char offset
148 charInfo
+= (characterToOutput
- fontInfo
->startChar
);
150 // get width from char info
151 charWidth
= charInfo
->widthBits
;
153 // get offset from char info
154 charOffset
= charInfo
->offset
;
158 // if no char info, char width is always 5
161 // char offset - assume 5 * letter offset
162 charOffset
= (characterToOutput
- fontInfo
->startChar
) * 5;
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
);
171 currentX
+= charWidth
+ 1;
178 /**************************************************************************/
180 @brief Returns the width in pixels of a string when it is rendered
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.
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
192 Pointer to the FONT_INFO for the font that will be used
194 The string that will be rendered
196 /**************************************************************************/
197 uint16_t fontsGetStringWidth(const FONT_INFO
*fontInfo
, char *str
)
201 uint32_t startChar
= fontInfo
->startChar
;
204 for (currChar
= *str
; currChar
; currChar
= *(++str
))
206 // if char info exists for the font, use width from there
207 if (fontInfo
->charInfo
!= NULL
)
209 width
+= fontInfo
->charInfo
[currChar
- startChar
].widthBits
+ 1;
217 /* return the width */
218 return width
> 0 ? width
- 1 : width
;
This page took 0.056038 seconds and 5 git commands to generate.