NXWidgets  1.19
ctext.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/ctext.hxx
3  *
4  * Copyright (C) 2012, 2016 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  * Portions of this package derive from Woopsi (http://woopsi.org/) and
37  * portions are original efforts. It is difficult to determine at this
38  * point what parts are original efforts and which parts derive from Woopsi.
39  * However, in any event, the work of Antony Dzeryn will be acknowledged
40  * in most NxWidget files. Thanks Antony!
41  *
42  * Copyright (c) 2007-2011, Antony Dzeryn
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions are met:
47  *
48  * * Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * * Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * * Neither the names "Woopsi", "Simian Zombie" nor the
54  * names of its contributors may be used to endorse or promote products
55  * derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  ****************************************************************************/
69 
70 #ifndef __INCLUDE_CTEXT_HXX
71 #define __INCLUDE_CTEXT_HXX
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <stdint.h>
80 #include <stdbool.h>
81 
82 #include <nuttx/nx/nxglib.h>
83 #include <nuttx/nx/nx.h>
84 
85 #include "nxconfig.hxx"
86 #include "cnxfont.hxx"
87 #include "tnxarray.hxx"
88 #include "cnxstring.hxx"
89 
90 /****************************************************************************
91  * Pre-Processor Definitions
92  ****************************************************************************/
93 
94 /****************************************************************************
95  * Implementation Classes
96  ****************************************************************************/
97 
98 #if defined(__cplusplus)
99 
100 namespace NXWidgets
101 {
102  /**
103  * This class functions as a wrapper around a char array offering
104  * more advanced functionality - it can wrap text, calculate its
105  * height in pixels, calculate the width of a row, etc.
106  */
107 
108  class CText : public CNxString
109  {
110  private:
111 
112  /**
113  * Struct defining the position and length of a longest line within
114  * the m_linePositions array.
115  */
116 
117  typedef struct
118  {
119  int index;
120  uint8_t width;
121  } LongestLine;
122 
123  CNxFont *m_font; /**< Font to be used for output */
124  TNxArray<int> m_linePositions; /**< Array containing start indexes
125  of each wrapped line */
126  TNxArray<LongestLine> m_longestLines; /**< Array containing data describing
127  successively longer wrapped
128  lines */
129  nxgl_coord_t m_lineSpacing; /**< Spacing between lines of text */
130  int32_t m_textPixelHeight; /**< Total height of the wrapped
131  text in pixels */
132  uint8_t m_textPixelWidth; /**< Total width of the wrapped text
133  in pixels */
134  nxgl_coord_t m_width; /**< Width in pixels available t
135  the text */
136 
137  public:
138 
139  /**
140  * Constructor.
141  *
142  * @param font The font to use for this text object.
143  * @param text A string that this text object should wrap around.
144  * @param width The pixel width at which the text should wrap.
145  */
146 
147  CText(CNxFont *font, const CNxString &text, nxgl_coord_t width);
148 
149  /**
150  * Destructor.
151  */
152 
153  virtual ~CText(void) {}
154 
155  /**
156  * Set the text in the string.
157  *
158  * @param text Char array to use as the new data for this string.
159  */
160 
161  virtual void setText(const CNxString &text);
162 
163  /**
164  * Set the text in the string.
165  *
166  * @param text Char array to use as the new data for this string.
167  */
168 
169  virtual void setText(FAR const char *text);
170 
171  /**
172  * Set the text in the string.
173  *
174  * @param text Character to to use as the new data for this string.
175  */
176 
177  virtual void setText(const nxwidget_char_t text);
178 
179  /**
180  * Append text to the end of the string.
181  *
182  * @param text String to append.
183  */
184 
185  virtual void append(const CNxString &text);
186 
187  /**
188  * Insert text at the specified character index.
189  *
190  * @param text The text to insert.
191  * @param index The char index to insert at.
192  */
193 
194  virtual void insert(const CNxString &text, const int index);
195 
196  /**
197  * Remove all characters from the string from the start index onwards.
198  *
199  * @param startIndex The char index to start removing from.
200  */
201 
202  virtual void remove(const int startIndex);
203 
204  /**
205  * Remove all characters from the string from the start index onwards.
206  *
207  * @param startIndex The char index to start removing from.
208  * @param count The number of chars to remove.
209  */
210 
211  virtual void remove(const int startIndex, const int count);
212 
213  /**
214  * Set the vertical spacing between rows of text.
215  *
216  * @param lineSpacing The line spacing.
217  */
218 
219  void setLineSpacing(nxgl_coord_t lineSpacing);
220 
221  /**
222  * Sets the pixel width of the text; text wider than
223  * this will automatically wrap.
224  *
225  * @param width Maximum pixel width of the text.
226  */
227 
228  void setWidth(nxgl_coord_t width);
229 
230  /**
231  * Set the font to use.
232  *
233  * @param font Pointer to the new font.
234  */
235 
236  void setFont(CNxFont *font);
237 
238  /**
239  * Get the number of characters in the specified line number.
240  *
241  * @param lineNumber The line number to check.
242  * @return The number of characters in the line.
243  */
244 
245  const int getLineLength(const int lineNumber) const;
246 
247  /**
248  * Get the number of characters in the specified line number,
249  * ignoring any trailing blank characters.
250  *
251  * @param lineNumber The line number to check.
252  * @return The number of characters in the line.
253  */
254 
255  const int getLineTrimmedLength(const int lineNumber) const;
256 
257  /**
258  * Get the width in pixels of the specified line number.
259  *
260  * @param lineNumber The line number to check.
261  * @return The pixel width of the line.
262  */
263 
264  const nxgl_coord_t getLinePixelLength(const int lineNumber) const;
265 
266  /**
267  * Get the width in pixels of the specified line number,
268  * ignoring any trailing blank characters.
269  *
270  * @param lineNumber The line number to check.
271  * @return The pixel width of the line.
272  */
273 
274  const nxgl_coord_t getLineTrimmedPixelLength(const int lineNumber) const;
275 
276  /**
277  * Get the total height of the text in pixels.
278  *
279  * @return The total height of the text.
280  */
281 
282  inline const int32_t getPixelHeight(void) const
283  {
284  return m_textPixelHeight;
285  }
286 
287  /**
288  * Get the width of the longest line in pixels.
289  *
290  * @return The width of the longest line.
291  */
292 
293  inline const uint8_t getPixelWidth(void) const
294  {
295  return m_textPixelWidth;
296  }
297 
298  /**
299  * Get the pixel spacing between each line of text.
300  *
301  * @return The line spacing.
302  */
303 
304  inline const uint8_t getLineSpacing(void) const
305  {
306  return m_lineSpacing;
307  }
308 
309  /**
310  * Get the height in pixels of a line, given as the
311  * height of the font plus the line spacing.
312  *
313  * @return The height of a line.
314  */
315 
316  inline const uint8_t getLineHeight(void) const
317  {
318  return m_font->getHeight() + m_lineSpacing;
319  }
320 
321  /**
322  * Get the total number of lines in the text.
323  *
324  * @return The line count.
325  */
326 
327  inline const int getLineCount(void) const
328  {
329  return m_linePositions.size() - 1;
330  }
331 
332  /**
333  * Get a pointer to the CText object's font.
334  *
335  * @return Pointer to the font.
336  */
337 
338  CNxFont *getFont(void) const;
339 
340  /**
341  * Removes lines of text from the start of the text buffer.
342  *
343  * @param lines Number of lines to remove
344  */
345 
346  void stripTopLines(const int lines);
347 
348  /**
349  * Wrap all of the text.
350  */
351 
352  void wrap(void);
353 
354  /**
355  * Wrap the text from the line containing the specified char index onwards.
356  *
357  * @param charIndex The index of the char to start wrapping from; note
358  * that the wrapping function will re-wrap that entire line of text.
359  */
360 
361  void wrap(int charIndex);
362 
363  /**
364  * Get the index of the line of text that contains the specified index
365  * within the raw char array.
366  *
367  * @param index The index to locate within the wrapped lines of text.
368  * @return The number of the line of wrapped text that contains the
369  * specified index.
370  */
371 
372  const int getLineContainingCharIndex(const int index) const;
373 
374  /**
375  * Gets the index within the char array that represents the start of the line of
376  * text indicated by the line parameter.
377  *
378  * @param line The line number to locate within the char array.
379  * @return The index within the char array of the start of the supplied line.
380  */
381 
382  const int getLineStartIndex(const int line) const
383  {
384  return m_linePositions[line];
385  }
386  };
387 }
388 
389 #endif // __cplusplus
390 
391 #endif // __INCLUDE_CTEXT_HXX
392 
const int getLineContainingCharIndex(const int index) const
Definition: ctext.cxx:609
const int getLineLength(const int lineNumber) const
Definition: ctext.cxx:240
nxgl_coord_t m_lineSpacing
Definition: ctext.hxx:129
const uint8_t getLineSpacing(void) const
Definition: ctext.hxx:304
const uint8_t getLineHeight(void) const
Definition: ctext.hxx:316
void stripTopLines(const int lines)
Definition: ctext.cxx:334
TNxArray< int > m_linePositions
Definition: ctext.hxx:124
const int getLineTrimmedLength(const int lineNumber) const
Definition: ctext.cxx:258
virtual ~CText(void)
Definition: ctext.hxx:153
CNxFont * m_font
Definition: ctext.hxx:123
virtual void setText(const CNxString &text)
Definition: ctext.cxx:115
const int32_t getPixelHeight(void) const
Definition: ctext.hxx:282
const nxgl_coord_t getLineTrimmedPixelLength(const int lineNumber) const
Definition: ctext.cxx:311
virtual void append(const CNxString &text)
Definition: ctext.cxx:151
int32_t m_textPixelHeight
Definition: ctext.hxx:130
const nxgl_coord_t getLinePixelLength(const int lineNumber) const
Definition: ctext.cxx:297
CText(CNxFont *font, const CNxString &text, nxgl_coord_t width)
Definition: ctext.cxx:100
uint16_t nxwidget_char_t
Definition: nxconfig.hxx:454
const int getLineStartIndex(const int line) const
Definition: ctext.hxx:382
nxgl_coord_t m_width
Definition: ctext.hxx:134
virtual void insert(const CNxString &text, const int index)
Definition: ctext.cxx:164
uint8_t m_textPixelWidth
Definition: ctext.hxx:132
CNxFont * getFont(void) const
Definition: ctext.cxx:323
void wrap(void)
Definition: ctext.cxx:358
void setFont(CNxFont *font)
Definition: ctext.cxx:227
const uint8_t getHeight(void) const
Definition: cnxfont.hxx:229
void setWidth(nxgl_coord_t width)
Definition: ctext.cxx:215
const uint8_t getPixelWidth(void) const
Definition: ctext.hxx:293
TNxArray< LongestLine > m_longestLines
Definition: ctext.hxx:126
const int getLineCount(void) const
Definition: ctext.hxx:327
const int size(void) const
Definition: tnxarray.hxx:261
void setLineSpacing(nxgl_coord_t lineSpacing)
Definition: ctext.cxx:202