Spinning Topp Logo BlackTopp Studios
inc
texttoken.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uitexttoken_h
41 #define _uitexttoken_h
42 
43 #include "datatypes.h"
44 
45 namespace Mezzanine
46 {
47  namespace UI
48  {
49  class MarkupParser;
50  ///////////////////////////////////////////////////////////////////////////////
51  /// @brief This class represents a normal text segment from the source string.
52  /// @details Text tokens are intended for the generation of segments of regular glyphs provided by an atlas
53  /// or font file. They have no special behaviors of their own and will almost always have their render size
54  /// match their text size.
55  ///////////////////////////////////////
57  {
58  public:
59  /// @brief The type of token this class represents.
60  enum TokenType
61  {
62  TT_Error = 0, ///< Used to describe any generic error with a token.
63  TT_Text = 1, ///< Used to describe a normal text token with plain text.
64  TT_TagInvalid = 2, ///< Used to describe a tag token that is syntactically correct, but has another error, such as a range tag missing a partner, or the tag name isn't found.
65  TT_RangeTagStart = 3, ///< Used to describe either a tag token inserting a character, or the start of a range tag.
66  TT_RangeTagEnd = 4, ///< Used to describe the end of a range tag.
67  TT_InsertTag = 5 ///< Used to describe a single tag used to insert a special character (such as s sprite).
68  };//TokenType
69  protected:
70  friend class MarkupParser;
71  /// @internal
72  /// @brief Type of token this is.
74  /// @internal
75  /// @brief The number of rendered characters this token produced.
77  /// @internal
78  /// @brief Container for the converted text.
80  /// @internal
81  /// @brief Takes a position of a renderable char and converts it to the respective position in the raw string.
82  /// @note In cases where ASCII is used, this will always return the same number, this exists for when more exotic unicode characters/glyphs are used.
83  /// @param Index The index to be converted.
84  /// @return Returns a UInt32 representing the Raw character index of the first character corresponding to the rendered character at the provided index.
85  UInt32 ConvertRenderIndexToRawIndex(const UInt32 Index);
86  public:
87  /// @brief Class constructor.
88  /// @note This is mostly used for inheritence purposes.
89  TextToken();
90  /// @brief Descriptive constructor.
91  /// @param RawText A string containing the raw text of this token.
92  /// @param TType The type of text token to be created.
93  TextToken(const String& RawText, const TokenType TType);
94  /// @brief Char8 constructor.
95  /// @param Characters A buffer of UTF-8 characters to populate this TextToken with.
96  /// @param Size The number of characters that exist in the provided buffer.
97  TextToken(const Char8* Characters, const UInt32 Size);
98  /// @brief Char8 constructor.
99  /// @param Characters A buffer of UTF-32 characters to populate this TextToken with.
100  /// @param Size The number of characters that exist in the provided buffer.
101  TextToken(const UInt32* Characters, const UInt32 Size);
102  /// @brief Class destructor.
103  virtual ~TextToken();
104 
105  ///////////////////////////////////////////////////////////////////////////////
106  // Utility
107 
108  /// @brief Gets the raw string for this token used to render or manipulate characters.
109  /// @return Returns a reference to the source string stored by this token.
110  virtual const String& GetRawCharacterString() const;
111  /// @brief Gets the type of token this is.
112  /// @return Returns a TokenType enum value describing the type of token this is.
113  virtual TextToken::TokenType GetTokenType() const;
114  /// @brief Gets the number of Char8's that comprise the source text for this tag.
115  /// @return Returns a UInt32 containing the size of the source string for this token.
116  virtual UInt32 GetRawCharacterSize() const;
117  /// @brief Gets the number of rendered characters this token generates.
118  /// @note This number is initially generated when the tokens are parsed and the actual characters are generated. Methods that modify
119  /// the source text try to keep this in sync appropriately.
120  /// @return Returns a UInt32 containing the number of characters are being rendered via the text from this token.
121  virtual UInt32 GetRenderCharacterSize() const;
122 
123  ///////////////////////////////////////////////////////////////////////////////
124  // Inserting and Removing
125 
126  /// @brief Inserts a single UTF-32 size character into this token.
127  /// @param Index The index at which the character will be inserted.
128  /// @param UChar The unicode character to be inserted. This will be converted to UTF-8 prior to insertion.
129  /// @return Returns 1 if the character was successfully inserted, otherwise returns 0. Note: This is not rendered characters, as they don't get generated until the tokens are re-parsed.
130  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
131  /// @brief Inserts multiple characters into this token.
132  /// @param Index The index at which the characters will be inserted.
133  /// @param Characters An array of Char8's encoded in UTF-8 to be inserted.
134  /// @param Size The size of the array of Char8's passed in.
135  /// @return Returns the number of characters successfully inserted. Note: This is not rendered characters, as they don't get generated until the tokens are re-parsed.
136  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
137  /// @brief Inserts multiple characters into this token.
138  /// @param Index The index at which the characters will be inserted.
139  /// @param Characters An array of UInt32's encoded in UTF-32 to be inserted.
140  /// @param Size The size of the array of UInt32's passed in.
141  /// @return Returns the number of characters successfully inserted. Note: This is not rendered characters, as they don't get generated until the tokens are re-parsed.
142  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
143  /// @brief Removes a single rendered character from this token.
144  /// @param Index The index at which the rendered character will be removed.
145  /// @return Returns 1 if the character was successfully removed, otherwise returns 0.
146  virtual UInt32 RemoveCharacter(const UInt32 Index);
147  /// @brief Removes rendered characters from this token.
148  /// @param Index The index at which to start removing characters from this token.
149  /// @param Length The number of characters to try and remove from this token.
150  /// @return Returns the number of rendered characters that were actually removed.
151  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
152  /// @brief Removes all the rendered characters from this token.
153  /// @return Returns the number of rendered characters that were actually removed.
154  virtual UInt32 ClearAllCharacters();
155  };//TextToken
156 
157  ///////////////////////////////////////////////////////////////////////////////
158  /// @brief This struct represents a markup tag segment from the source string.
159  /// @details Tag tokens are tokens that represent text that isn't intended to be rendered, but instead alter
160  /// other text around it or create special characters of their own. The render size on tag token should never
161  /// never match the text size, instead being 0 or 1.
162  ///////////////////////////////////////
163  class MEZZ_LIB TagToken : public TextToken
164  {
165  protected:
166  friend class MarkupParser;
167  /// @brief Unconverted version of the tag name.
169  /// @brief The parameters provided for this tag, if any.
171  public:
172  /// @brief Class constructor.
173  TagToken();
174  /// @brief Descriptive constructor.
175  /// @param RawText A string containing the raw text of this token.
176  /// @param Name The name of this tag.
177  /// @param TType The type of text token to be created.
178  TagToken(const String& RawText, const String& Name, const TokenType TType);
179  /// @brief Class destructor.
180  virtual ~TagToken();
181 
182  ///////////////////////////////////////////////////////////////////////////////
183  // Utility
184 
185  /// @brief Gets the name of the tag this token represents.
186  /// @return Returns a reference to a string containing the name of this tokens tag.
187  const String& GetTagName() const;
188  /// @brief Gets a parameter specified in this token by name.
189  /// @param Param The name of the parameter to retrieve.
190  /// @return Returns a string containing the value specified in this token.
191  String GetParameter(const String& Param) const;
192  };//TagToken
193 
194  ///////////////////////////////////////////////////////////////////////////////
195  /// @brief This struct represents a markup tag segment from the source string.
196  /// @details Tag tokens are tokens that represent text that isn't intended to be rendered, but instead alter
197  /// other text around it or create special characters of their own. The render size on tag token should never
198  /// never match the text size, instead being 0 or 1.
199  ///////////////////////////////////////
201  {
202  protected:
203  friend class MarkupParser;
204  /// @brief Pointer to the opening/closing tag for this tag.
206  public:
207  /// @brief Class constructor.
208  RangeTagToken();
209  /// @brief Descriptive constructor.
210  /// @param RawText A string containing the raw text of this token.
211  /// @param Name The name of this tag.
212  /// @param TType The type of text token to be created.
213  RangeTagToken(const String& RawText, const String& Name, const TokenType TType);
214  /// @brief Class destructor.
215  virtual ~RangeTagToken();
216 
217  ///////////////////////////////////////////////////////////////////////////////
218  // Utility
219 
220  /// @brief Gets the partner tag to this RangeTag
221  /// @return Returns a pointer to the other tag paired with this range tag.
222  RangeTagToken* GetPartnerTag() const;
223 
224  ///////////////////////////////////////////////////////////////////////////////
225  // Inserting and Removing
226 
227  /// @copydoc TextToken::InsertCharacter(const UInt32, UInt32)
228  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
229  /// @copydoc TextToken::InsertCharacters(const UInt32, const Char8*, const UInt32)
230  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
231  /// @copydoc TextToken::InsertCharacters(const UInt32, const UInt32*, const UInt32)
232  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
233  /// @copydoc TextToken::RemoveCharacter(const UInt32)
234  virtual UInt32 RemoveCharacter(const UInt32 Index);
235  /// @copydoc TextToken::RemoveCharacters(const UInt32, const UInt32)
236  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
237  /// @copydoc TextToken::ClearAllCharacters()
238  virtual UInt32 ClearAllCharacters();
239  };//RangeTagToken
240 
241  ///////////////////////////////////////////////////////////////////////////////
242  /// @brief This struct represents a markup tag segment from the source string.
243  /// @details Tag tokens are tokens that represent text that isn't intended to be rendered, but instead alter
244  /// other text around it or create special characters of their own. The render size on tag token should never
245  /// never match the text size, instead being 0 or 1.
246  ///////////////////////////////////////
248  {
249  protected:
250  friend class MarkupParser;
251  public:
252  /// @brief Class constructor.
253  InsertTagToken();
254  /// @brief Descriptive constructor.
255  /// @param RawText A string containing the raw text of this token.
256  /// @param Name The name of this tag.
257  InsertTagToken(const String& RawText, const String& Name);
258  /// @brief Class destructor.
259  virtual ~InsertTagToken();
260 
261  ///////////////////////////////////////////////////////////////////////////////
262  // Inserting and Removing
263 
264  /// @copydoc TextToken::InsertCharacter(const UInt32, UInt32)
265  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
266  /// @copydoc TextToken::InsertCharacters(const UInt32, Char8*, const UInt32)
267  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
268  /// @copydoc TextToken::InsertCharacters(const UInt32, const UInt32*, const UInt32)
269  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
270  /// @copydoc TextToken::RemoveCharacter(const UInt32)
271  virtual UInt32 RemoveCharacter(const UInt32 Index);
272  /// @copydoc TextToken::RemoveCharacters(const UInt32, const UInt32)
273  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
274  /// @copydoc TextToken::ClearAllCharacters()
275  virtual UInt32 ClearAllCharacters();
276  };//InsertTagToken
277 
278  ///////////////////////////////////////////////////////////////////////////////
279  /// @brief This is a helper class that facilitates operations with collections of tokens generated from Markup Parsers.
280  /// @details
281  ///////////////////////////////////////
283  {
284  public:
285  /// @brief Basic container type for the storage of @ref TextToken instances by this class.
286  typedef std::vector<TextToken*> TokenContainer;
287  /// @brief Iterator type for @ref TextToken instances being stored by this class.
288  typedef TokenContainer::iterator TokenIterator;
289  /// @brief Const Iterator type for @ref TextToken instances being stored by this class.
290  typedef TokenContainer::const_iterator ConstTokenIterator;
291  /// @brief Reverse Iterator type for @ref TextToken instances being stored by this class.
292  typedef TokenContainer::reverse_iterator ReverseTokenIterator;
293  /// @brief Const Reverse Iterator type for @ref TextToken instances being stored by this class.
294  typedef TokenContainer::const_reverse_iterator ConstReverseTokenIterator;
295  /// @brief An std::pair used to report the result of a TextToken and it's local index.
296  typedef std::pair<TokenIterator,UInt32> TokenIndexPair;
297  protected:
298  /// @internal
299  /// @brief Container for TextToken storage.
300  TokenContainer Tokens;
301  /// @internal
302  /// @brief Gets the token at the string index, and it's local index.
303  /// @param Index The TokenString index of the character to retrieve.
304  /// @return Returns an std::pair containing an iterator to the token at the specified index in the first member, and it's local index in the second member
305  TokenIndexPair GetTokenIndex(const UInt32 Index);
306  public:
307  /// @brief Class constructor.
308  TokenString();
309  /// @brief Class destructor.
310  virtual ~TokenString();
311 
312  ///////////////////////////////////////////////////////////////////////////////
313  // Utility
314 
315  /// @brief Gets a string containing all the raw characters of the tokens in this string.
316  /// @return Returns a string of all the UTF-8 characters in the tokens of this string.
317  virtual String GetRawString() const;
318  /// @brief Gets the number of renderable characters that exist in this string.
319  /// @return Returns a Whole containing the number of renderable characters this TokenString is storing.
320  Whole GetNumCharacters() const;
321  /// @brief Gets the number of text tokens in this string.
322  /// @return Returns a Whole containing the number of tokens this TokenString is storing.
323  Whole GetNumTokens() const;
324 
325  /// @brief Gets an iterator to the first TextToken.
326  /// @return Returns an iterator to the first TextToken being stored by this TokenString.
327  TokenIterator BeginToken();
328  /// @brief Gets an iterator to one passed the last TextToken.
329  /// @return Returns an iterator to one passed the last TextToken being stored by this TokenString.
330  TokenIterator EndToken();
331  /// @brief Gets a const iterator to the first TextToken.
332  /// @return Returns a const iterator to the first TextToken being stored by this TokenString.
333  ConstTokenIterator BeginToken() const;
334  /// @brief Gets a const iterator to one passed the last TextToken.
335  /// @return Returns a const iterator to one passed the last TextToken being stored by this TokenString.
336  ConstTokenIterator EndToken() const;
337 
338  /// @brief Gets a reverse iterator to the last TextToken.
339  /// @return Returns a reverse iterator to the last TextToken being stored by this TokenString.
340  ReverseTokenIterator ReverseBeginToken();
341  /// @brief Gets a reverse iterator to one before the first TextToken.
342  /// @return Returns a reverse iterator to one before the first TextToken being stored by this TokenString.
343  ReverseTokenIterator ReverseEndToken();
344  /// @brief Gets a const reverse iterator to the last TextToken.
345  /// @return Returns a const reverse iterator to the last TextToken being stored by this TokenString.
346  ConstReverseTokenIterator ReverseBeginToken() const;
347  /// @brief Gets a const reverse iterator to one before the first TextToken.
348  /// @return Returns a const reverse iterator to one before the first TextToken being stored by this TokenString.
349  ConstReverseTokenIterator ReverseEndToken() const;
350 
351  ///////////////////////////////////////////////////////////////////////////////
352  // Generating and Destroying the String
353 
354  /// @brief Appends a new token to the end of this string.
355  /// @note Tokens appended to this string will become owned by this string and deleted when this string is destructed.
356  /// @param ToBePushed The token being added to this string.
357  virtual void PushToken(TextToken* ToBePushed);
358  /// @brief Appends a group of tokens to the end of this string.
359  /// @note Tokens appended to this string will become owned by this string and deleted when this string is destructed.
360  /// @param ToBePushed A container
361  virtual void PushTokens(const TokenContainer& ToBePushed);
362  /// @brief Destroys all tokens currently in this string.
363  virtual void DestroyAllTokens();
364 
365  ///////////////////////////////////////////////////////////////////////////////
366  // Inserting and Removing
367 
368  /// @brief Inserts a single UTF-32 size character into this string.
369  /// @param Index The index at which the character will be inserted.
370  /// @param UChar The unicode character to be inserted. This will be converted to UTF-8 prior to insertion.
371  /// @return Returns 1 if the character was successfully inserted, otherwise returns 0.
372  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
373  /// @brief Inserts multiple characters into this string.
374  /// @param Index The index at which the characters will be inserted.
375  /// @param Characters An array of Char8's encoded in UTF-8 to be inserted.
376  /// @param Size The size of the array of Char8's passed in.
377  /// @return Returns the number of characters successfully inserted.
378  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
379  /// @brief Inserts multiple characters into this string.
380  /// @param Index The index at which the characters will be inserted.
381  /// @param Characters An array of UInt32's encoded in UTF-32 to be inserted.
382  /// @param Size The size of the array of UInt32's passed in.
383  /// @return Returns the number of characters successfully inserted.
384  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
385  /// @brief Removes a single rendered character from this string.
386  /// @param Index The index at which the rendered character will be removed.
387  /// @return Returns 1 if the character was successfully removed, otherwise returns 0.
388  virtual UInt32 RemoveCharacter(const UInt32 Index);
389  /// @brief Removes rendered characters from this string.
390  /// @param Index The index at which to start removing characters from this string.
391  /// @param Length The number of characters to try and remove from this string.
392  /// @return Returns the number of rendered characters that were actually removed.
393  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
394  /// @brief Removes all the rendered characters from this string.
395  /// @return Returns the number of rendered characters that were actually removed.
396  virtual UInt32 ClearAllCharacters();
397  };//TokenString
398  }//UI
399 }//Mezzanine
400 
401 #endif
TokenContainer Tokens
Container for TextToken storage.
Definition: texttoken.h:300
This class represents a normal text segment from the source string.
Definition: texttoken.h:56
NameValuePairMap Params
The parameters provided for this tag, if any.
Definition: texttoken.h:170
String TagName
Unconverted version of the tag name.
Definition: texttoken.h:168
This struct represents a markup tag segment from the source string.
Definition: texttoken.h:247
This is a helper class that facilitates operations with collections of tokens generated from Markup P...
Definition: texttoken.h:282
UInt32 RenderSize
The number of rendered characters this token produced.
Definition: texttoken.h:76
All the definitions for datatypes as well as some basic conversion functions are defined here...
std::pair< TokenIterator, UInt32 > TokenIndexPair
An std::pair used to report the result of a TextToken and it's local index.
Definition: texttoken.h:296
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
TokenContainer::reverse_iterator ReverseTokenIterator
Reverse Iterator type for TextToken instances being stored by this class.
Definition: texttoken.h:292
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
std::vector< TextToken * > TokenContainer
Basic container type for the storage of TextToken instances by this class.
Definition: texttoken.h:286
TokenType
The type of token this class represents.
Definition: texttoken.h:60
This struct represents a markup tag segment from the source string.
Definition: texttoken.h:200
This struct represents a markup tag segment from the source string.
Definition: texttoken.h:163
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
RangeTagToken * PartnerTag
Pointer to the opening/closing tag for this tag.
Definition: texttoken.h:205
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
TokenContainer::iterator TokenIterator
Iterator type for TextToken instances being stored by this class.
Definition: texttoken.h:288
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
TokenContainer::const_iterator ConstTokenIterator
Const Iterator type for TextToken instances being stored by this class.
Definition: texttoken.h:290
This is a base class for the parsing of markup texts contained in text layers.
Definition: markupparser.h:126
TokenType Type
Type of token this is.
Definition: texttoken.h:73
String Text
Container for the converted text.
Definition: texttoken.h:79
std::map< String, String > NameValuePairMap
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:209
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
TokenContainer::const_reverse_iterator ConstReverseTokenIterator
Const Reverse Iterator type for TextToken instances being stored by this class.
Definition: texttoken.h:294