NXWidgets  1.19
cnxfont.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cnxfont.hxx
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 #ifndef __INCLUDE_CNXFONT_HXX
37 #define __INCLUDE_CNXFONT_HXX
38 
39 /****************************************************************************
40  * Included Files
41  ****************************************************************************/
42 
43 #include <nuttx/config.h>
44 
45 #include <stdint.h>
46 #include <stdbool.h>
47 
48 #include <nuttx/nx/nxglib.h>
49 #include <nuttx/nx/nxfonts.h>
50 
51 /****************************************************************************
52  * Pre-Processor Definitions
53  ****************************************************************************/
54 
55 /****************************************************************************
56  * Implementation Classes
57  ****************************************************************************/
58 
59 #if defined(__cplusplus)
60 
61 namespace NXWidgets
62 {
63  class CNxString;
64  struct SBitmap;
65 
66  /**
67  * Class defining the properties of one font.
68  */
69 
70  class CNxFont
71  {
72  private:
73  enum nx_fontid_e m_fontId; /**< The font ID. */
74  NXHANDLE m_fontHandle; /**< The font handle */
75  FAR const struct nx_font_s *m_pFontSet; /** < The font set metrics */
76  nxgl_mxpixel_t m_fontColor; /**< Color to draw the font with when rendering. */
77  nxgl_mxpixel_t m_transparentColor; /**< Background color that should not be rendered. */
78 
79  public:
80 
81  /**
82  * CNxFont Constructor.
83  *
84  * @param fontid The font ID to use.
85  * @param fontColor The font color to use.
86  * @param transparentColor The color in the font bitmap used as the
87  * background color.
88  */
89 
90  CNxFont(enum nx_fontid_e fontid, nxgl_mxpixel_t fontColor,
91  nxgl_mxpixel_t transparentColor);
92 
93  /**
94  * CNxFont Destructor.
95  */
96 
97  ~CNxFont() { }
98 
99  /**
100  * Checks if supplied character is blank in the current font.
101  *
102  * @param letter The character to check.
103  * @return True if the glyph contains any pixels to be drawn. False if
104  * the glyph is blank.
105  */
106 
107  const bool isCharBlank(const nxwidget_char_t letter) const;
108 
109  /**
110  * Gets the color currently being used as the drawing color.
111  *
112  * @return The current drawing color.
113  */
114 
115  inline const nxgl_mxpixel_t getColor() const
116  {
117  return m_fontColor;
118  }
119 
120  /**
121  * Sets the color to use as the drawing color. If set, this overrides
122  * the colors present in a non-monochrome font.
123  * @param color The new drawing color.
124  */
125 
126  inline void setColor(const nxgl_mxpixel_t color)
127  {
128  m_fontColor = color;
129  }
130 
131  /**
132  * Get the color currently being used as the transparent background
133  * color.
134  * @return The transparent background color.
135  */
136 
137  inline const nxgl_mxpixel_t getTransparentColor() const
138  {
139  return m_transparentColor;
140  }
141 
142  /**
143  * Sets the transparent background color to a new value.
144  * @param color The new background color.
145  */
146 
147  inline void setTransparentColor(const nxgl_mxpixel_t color)
148  {
149  m_transparentColor = color;
150  }
151 
152  /**
153  * Draw an individual character of the font to the specified bitmap.
154  *
155  * @param bitmap The bitmap to draw to.
156  * @param letter The character to output.
157  */
158 
159  void drawChar(FAR SBitmap *bitmap, nxwidget_char_t letter);
160 
161  /**
162  * Get the width of a string in pixels when drawn with this font.
163  *
164  * @param text The string to check.
165  * @return The width of the string in pixels.
166  */
167 
168  nxgl_coord_t getStringWidth(const CNxString &text) const;
169 
170  /**
171  * Get the width of a portion of a string in pixels when drawn with this
172  * font.
173  *
174  * @param text The string to check.
175  * @param startIndex The start point of the substring within the string.
176  * @param length The length of the substring in chars.
177  * @return The width of the substring in pixels.
178  */
179 
180  nxgl_coord_t getStringWidth(const CNxString& text,
181  int startIndex, int length) const;
182 
183  /**
184  * Gets font metrics for a particular character
185  *
186  *
187  * @param letter The character to get the width of.
188  * @param metrics The location to return the font metrics
189  */
190 
191  void getCharMetrics(nxwidget_char_t letter,
192  FAR struct nx_fontmetric_s *metrics) const;
193 
194  /**
195  * Get the width of an individual character.
196  *
197  * @param letter The character to get the width of.
198  * @return The width of the character in pixels.
199  */
200 
201  nxgl_coord_t getCharWidth(nxwidget_char_t letter) const;
202 
203  /**
204  * Get the height of an individual character.
205  *
206  * @param letter The letter to get the height of.
207  * @return The height of the character in pixels.
208  */
209 
210  inline nxgl_coord_t getCharHeight(nxwidget_char_t letter) const;
211 
212  /**
213  * Gets the maximum width of the font.
214  *
215  * @return The height of the font.
216  */
217 
218  inline const uint8_t getMaxWidth(void) const
219  {
220  return m_pFontSet->mxwidth;
221  }
222 
223  /**
224  * Gets the height of the font.
225  *
226  * @return The height of the font.
227  */
228 
229  inline const uint8_t getHeight(void) const
230  {
231  return m_pFontSet->mxheight;
232  }
233  };
234 }
235 
236 #endif // __cplusplus
237 
238 #endif // __INCLUDE_CNXFONT_HXX
nxgl_coord_t getCharWidth(nxwidget_char_t letter) const
Definition: cnxfont.cxx:254
nxgl_mxpixel_t m_transparentColor
Definition: cnxfont.hxx:77
NXHANDLE m_fontHandle
Definition: cnxfont.hxx:74
nxgl_coord_t getStringWidth(const CNxString &text) const
Definition: cnxfont.cxx:142
const nxgl_mxpixel_t getColor() const
Definition: cnxfont.hxx:115
const uint8_t getMaxWidth(void) const
Definition: cnxfont.hxx:218
void setColor(const nxgl_mxpixel_t color)
Definition: cnxfont.hxx:126
enum nx_fontid_e m_fontId
Definition: cnxfont.hxx:73
const nxgl_mxpixel_t getTransparentColor() const
Definition: cnxfont.hxx:137
uint16_t nxwidget_char_t
Definition: nxconfig.hxx:454
nxgl_mxpixel_t m_fontColor
Definition: cnxfont.hxx:76
void drawChar(FAR SBitmap *bitmap, nxwidget_char_t letter)
Definition: cnxfont.cxx:114
const uint8_t getHeight(void) const
Definition: cnxfont.hxx:229
CNxFont(enum nx_fontid_e fontid, nxgl_mxpixel_t fontColor, nxgl_mxpixel_t transparentColor)
Definition: cnxfont.cxx:75
const bool isCharBlank(const nxwidget_char_t letter) const
Definition: cnxfont.cxx:93
FAR const struct nx_font_s * m_pFontSet
Definition: cnxfont.hxx:75
static const NXWidgets::SRlePaletteBitmapEntry bitmap[]
nxgl_coord_t getCharHeight(nxwidget_char_t letter) const
void setTransparentColor(const nxgl_mxpixel_t color)
Definition: cnxfont.hxx:147
void getCharMetrics(nxwidget_char_t letter, FAR struct nx_fontmetric_s *metrics) const
Definition: cnxfont.cxx:220