NXWidgets: cnxfont.cxx Source File

NXWidgets 1.19

cnxfont.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cnxfont.cxx
3 *
4 * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18 * me be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39
40 #include <nuttx/config.h>
41
42 #include <sys/types.h>
43 #include <stdint.h>
44 #include <stdbool.h>
45 #include <cstring>
46
47 #include <nuttx/nx/nxglib.h>
48 #include <nuttx/nx/nxfonts.h>
49
50 #include "nxconfig.hxx"
51 #include "cnxstring.hxx"
52 #include "cstringiterator.hxx"
53 #include "cnxfont.hxx"
54 #include "cbitmap.hxx"
55
56 /****************************************************************************
57 * Pre-Processor Definitions
58 ****************************************************************************/
59
60 /****************************************************************************
61 * CNxFont Method Implementations
62 ****************************************************************************/
63
64 using namespace NXWidgets;
65
66 /**
67 * CNxFont Constructor.
68 *
69 * @param fontid The font ID to use.
70 * @param fontColor The font color to use.
71 * @param transparentColor The color in the font bitmap used as the
72 * background color.
73 */
74
75 CNxFont::CNxFont(enum nx_fontid_e fontid, nxgl_mxpixel_t fontColor,
76 nxgl_mxpixel_t transparentColor)
77 {
78 m_fontId = fontid;
79 m_fontHandle = nxf_getfonthandle(fontid);
80 m_pFontSet = nxf_getfontset(m_fontHandle);
81 m_fontColor = fontColor;
82 m_transparentColor = transparentColor;
83 }
84
85 /**
86 * Checks if supplied character is blank in the current font.
87 *
88 * @param letter The character to check.
89 * @return True if the glyph contains any pixels to be drawn. False if
90 * the glyph is blank.
91 */
92
93 const bool CNxFont::isCharBlank(const nxwidget_char_t letter) const
94 {
95 FAR const struct nx_fontbitmap_s *fbm;
96
97 /* Get the bitmap associated with the character */
98
99 fbm = nxf_getbitmap(m_fontHandle, (uint16_t)letter);
100 return (fbm == NULL);
101 }
102
103 /**
104 * Draw an individual character of the font to the specified bitmap.
105 *
106 * @param bitmap The bitmap to draw use. The caller should use
107 * the getFontMetrics method to assure that the buffer will hold
108 * the font.
109 * @param letter The character to output.
110 *
111 * @return The width of the string in pixels.
112 */
113
114 void CNxFont::drawChar(FAR SBitmap *bitmap, nxwidget_char_t letter)
115 {
116 // Get the NX bitmap associated with the font
117
118 FAR const struct nx_fontbitmap_s *fbm;
119 fbm = nxf_getbitmap(m_fontHandle, letter);
120 if (fbm)
121 {
122 // Get information about the font bitmap
123
124 uint8_t fwidth = fbm->metric.width + fbm->metric.xoffset;
125 uint8_t fheight = fbm->metric.height + fbm->metric.yoffset;
126 uint8_t fstride = (fwidth * bitmap->bpp + 7) >> 3;
127
128 // Then render the glyph into the bitmap memory
129
130 (void)FONT_RENDERER((FAR nxgl_mxpixel_t*)bitmap->data, fheight,
131 fwidth, fstride, fbm, m_fontColor);
132 }
133 }
134
135 /**
136 * Get the width of a string in pixels when drawn with this font.
137 *
138 * @param text The string to check.
139 * @return The width of the string in pixels.
140 */
141
142 nxgl_coord_t CNxFont::getStringWidth(const CNxString &text) const
143 {
144 CStringIterator *iter = text.newStringIterator();
145
146 // Get the width of the string of characters
147 // Move to the first character
148
149 unsigned int width = 0;
150 if (iter->moveToFirst())
151 {
152 // moveToFirst returns true if there is at least one character
153
154 do
155 {
156 // Add the width of the font bitmap for this character and
157 // move the the next character
158
159 nxwidget_char_t ch = iter->getChar();
160
161 width += getCharWidth(ch);
162 }
163 while (iter->moveToNext());
164 }
165
166 // Return the total width
167
168 delete iter;
169 return width;
170 }
171
172 /**
173 * Get the width of a portion of a string in pixels when drawn with this
174 * font.
175 *
176 * @param text The string to check.
177 * @param startIndex The start point of the substring within the string.
178 * @param length The length of the substring in chars.
179 * @return The width of the substring in pixels.
180 */
181
182 nxgl_coord_t CNxFont::getStringWidth(const CNxString &text,
183 int startIndex, int length) const
184 {
185 CStringIterator *iter = text.newStringIterator();
186
187 // Get the width of the string of characters
188
189 iter->moveTo(startIndex);
190 unsigned int width = 0;
191
192 while (length-- > 0)
193 {
194 // Add the width of the font bitmap for this character
195
196 nxwidget_char_t ch = iter->getChar();
197 width += getCharWidth(ch);
198
199 // Position to the next character in the string
200
201 if (!iter->moveToNext())
202 {
203 break;
204 }
205 }
206
207 // Return the total width
208
209 delete iter;
210 return width;
211 }
212
213 /**
214 * Gets font metrics for a particular character
215 *
216 * @param letter The character to get the width of.
217 * @param metrics The location to return the font metrics
218 */
219
220 void CNxFont::getCharMetrics(nxwidget_char_t letter,
221 FAR struct nx_fontmetric_s *metrics) const
222 {
223 FAR const struct nx_fontbitmap_s *fbm;
224
225 // Get the font bitmap for this character
226
227 fbm = nxf_getbitmap(m_fontHandle, letter);
228 if (fbm)
229 {
230 // Return the metrics of the font
231
232 memcpy(metrics, &fbm->metric, sizeof(struct nx_fontmetric_s));
233 }
234 else
235 {
236 // Use the metrics of a space which has no width and no height
237 // but an xoffset equal to the standard width of a space.
238
239 metrics->stride = (m_pFontSet->spwidth * CONFIG_NXWIDGETS_BPP + 7) >> 3;
240 metrics->width = 0;
241 metrics->height = 0;
242 metrics->xoffset = m_pFontSet->spwidth;
243 metrics->yoffset = 0;
244 }
245 }
246
247 /**
248 * Get the width of an individual character.
249 *
250 * @param letter The character to get the width of.
251 * @return The width of the character in pixels.
252 */
253
254 nxgl_coord_t CNxFont::getCharWidth(nxwidget_char_t letter) const
255 {
256 FAR const struct nx_fontbitmap_s *fbm;
257 nxgl_coord_t width;
258
259 /* Get the font bitmap for this character */
260
261 fbm = nxf_getbitmap(m_fontHandle, letter);
262 if (fbm)
263 {
264 /* Return the width of the font -- including the X offset */
265
266 width = fbm->metric.width + fbm->metric.xoffset;
267 }
268 else
269 {
270 /* Use the width of a space */
271
272 width = m_pFontSet->spwidth;
273 }
274
275 return width;
276 }
NXWidgets::CStringIterator
Definition: cstringiterator.hxx:102
NXWidgets::CNxFont::getCharWidth
nxgl_coord_t getCharWidth(nxwidget_char_t letter) const
Definition: cnxfont.cxx:254
NXWidgets::CNxFont::m_transparentColor
nxgl_mxpixel_t m_transparentColor
Definition: cnxfont.hxx:77
NXWidgets::CNxFont::m_fontHandle
NXHANDLE m_fontHandle
Definition: cnxfont.hxx:74
cstringiterator.hxx
NXWidgets::CNxFont::getStringWidth
nxgl_coord_t getStringWidth(const CNxString &text) const
Definition: cnxfont.cxx:142
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxFont::m_fontId
enum nx_fontid_e m_fontId
Definition: cnxfont.hxx:73
NXWidgets::CStringIterator::moveToFirst
bool moveToFirst(void)
Definition: cstringiterator.cxx:111
NXWidgets::CStringIterator::moveToNext
bool moveToNext(void)
Definition: cstringiterator.cxx:149
NXWidgets::nxwidget_char_t
uint16_t nxwidget_char_t
Definition: nxconfig.hxx:454
nxconfig.hxx
NXWidgets::CNxFont::m_fontColor
nxgl_mxpixel_t m_fontColor
Definition: cnxfont.hxx:76
NXWidgets::CStringIterator::getChar
nxwidget_char_t getChar(void) const
Definition: cstringiterator.hxx:183
NXWidgets::CNxFont::drawChar
void drawChar(FAR SBitmap *bitmap, nxwidget_char_t letter)
Definition: cnxfont.cxx:114
NXWidgets::CNxFont::CNxFont
CNxFont(enum nx_fontid_e fontid, nxgl_mxpixel_t fontColor, nxgl_mxpixel_t transparentColor)
Definition: cnxfont.cxx:75
NXWidgets::CNxFont::isCharBlank
const bool isCharBlank(const nxwidget_char_t letter) const
Definition: cnxfont.cxx:93
NXWidgets::CNxFont::m_pFontSet
FAR const struct nx_font_s * m_pFontSet
Definition: cnxfont.hxx:75
cnxfont.hxx
cbitmap.hxx
NXWidgets::CNxString::newStringIterator
CStringIterator * newStringIterator(void) const
Definition: cnxstring.cxx:156
NXWidgets::CNxString
Definition: cnxstring.hxx:121
NXWidgets::SBitmap
Definition: cbitmap.hxx:103
bitmap
static const NXWidgets::SRlePaletteBitmapEntry bitmap[]
Definition: glyph_mediaplayer24x24.cxx:85
NXWidgets::CONFIG_NXWIDGETS_BPP
CONFIG_NXWIDGETS_BPP
Definition: glyph_arrowdown.cxx:146
NXWidgets::CStringIterator::moveTo
bool moveTo(int index)
Definition: cstringiterator.cxx:185
cnxstring.hxx
NXWidgets::CNxFont::getCharMetrics
void getCharMetrics(nxwidget_char_t letter, FAR struct nx_fontmetric_s *metrics) const
Definition: cnxfont.cxx:220