MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
texttoken.h
1 //© Copyright 2010 - 2012 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  /// @enum TokenType
60  /// @brief The type of token this class represents.
61  enum TokenType
62  {
63  TT_Error = 0, ///< Used to describe any generic error with a token.
64  TT_Text = 1, ///< Used to describe a normal text token with plain text.
65  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.
66  TT_RangeTagStart = 3, ///< Used to describe either a tag token inserting a character, or the start of a range tag.
67  TT_RangeTagEnd = 4, ///< Used to describe the end of a range tag.
68  TT_InsertTag = 5 ///< Used to describe a single tag used to insert a special character (such as s sprite).
69  };//TokenType
70  protected:
71  friend class MarkupParser;
72  /// @internal
73  /// @brief Type of token this is.
75  /// @internal
76  /// @brief The number of rendered characters this token produced.
78  /// @internal
79  /// @brief Container for the converted text.
81  /// @internal
82  /// @brief Takes a position of a renderable char and converts it to the respective position in the raw string.
83  /// @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.
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.
90  Type(TT_Error), RenderSize(0) { }
91  /// @brief Descriptive constructor.
92  /// @param RawText A string containing the raw text of this token.
93  /// @param TType The type of text token to be created.
94  TextToken(const String& RawText, const TokenType TType) :
95  Type(TType), RenderSize(0), Text(RawText) { }
96  /// @brief Char8 constructor.
97  /// @param Characters A buffer of UTF-8 characters to populate this TextToken with.
98  /// @param Size The number of characters that exist in the provided buffer.
99  TextToken(const Char8* Characters, const UInt32 Size) :
100  Type(TT_Text), RenderSize(0) { this->InsertCharacters(0,Characters,Size); }
101  /// @brief Char8 constructor.
102  /// @param Characters A buffer of UTF-32 characters to populate this TextToken with.
103  /// @param Size The number of characters that exist in the provided buffer.
104  TextToken(const UInt32* Characters, const UInt32 Size) :
105  Type(TT_Text), RenderSize(0) { this->InsertCharacters(0,Characters,Size); }
106  /// @brief Class destructor.
107  virtual ~TextToken()
108  { }
109 
110  ///////////////////////////////////////////////////////////////////////////////
111  // Utility
112 
113  /// @brief Gets the raw string for this token used to render or manipulate characters.
114  /// @return Returns a reference to the source string stored by this token.
115  virtual const String& GetRawCharacterString() const;
116  /// @brief Gets the type of token this is.
117  /// @return Returns a TokenType enum value describing the type of token this is.
118  virtual TextToken::TokenType GetTokenType() const;
119  /// @brief Gets the number of Char8's that comprise the source text for this tag.
120  /// @return Returns a UInt32 containing the size of the source string for this token.
121  virtual UInt32 GetRawCharacterSize() const;
122  /// @brief Gets the number of rendered characters this token generates.
123  /// @note This number is initially generated when the tokens are parsed and the actual characters are generated. Methods that modify
124  /// the source text try to keep this in sync appropriately.
125  /// @return Returns a UInt32 containing the number of characters are being rendered via the text from this token.
126  virtual UInt32 GetRenderCharacterSize() const;
127 
128  ///////////////////////////////////////////////////////////////////////////////
129  // Inserting and Removing
130 
131  /// @brief Inserts a single UTF-32 size character into this token.
132  /// @param Index The index at which the character will be inserted.
133  /// @param UChar The unicode character to be inserted. This will be converted to UTF-8 prior to insertion.
134  /// @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.
135  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
136  /// @brief Inserts multiple characters into this token.
137  /// @param Index The index at which the characters will be inserted.
138  /// @param Characters An array of Char8's encoded in UTF-8 to be inserted.
139  /// @param Size The size of the array of Char8's passed in.
140  /// @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.
141  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
142  /// @brief Inserts multiple characters into this token.
143  /// @param Index The index at which the characters will be inserted.
144  /// @param Characters An array of UInt32's encoded in UTF-32 to be inserted.
145  /// @param Size The size of the array of UInt32's passed in.
146  /// @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.
147  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
148  /// @brief Removes a single rendered character from this token.
149  /// @param Index The index at which the rendered character will be removed.
150  /// @return Returns 1 if the character was successfully removed, otherwise returns 0.
151  virtual UInt32 RemoveCharacter(const UInt32 Index);
152  /// @brief Removes rendered characters from this token.
153  /// @param Index The index at which to start removing characters from this token.
154  /// @param Length The number of characters to try and remove from this token.
155  /// @return Returns the number of rendered characters that were actually removed.
156  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
157  /// @brief Removes all the rendered characters from this token.
158  /// @return Returns the number of rendered characters that were actually removed.
159  virtual UInt32 ClearAllCharacters();
160  };//TextToken
161 
162  ///////////////////////////////////////////////////////////////////////////////
163  /// @brief This struct represents a markup tag segment from the source string.
164  /// @details Tag tokens are tokens that represent text that isn't intended to be rendered, but instead alter
165  /// other text around it or create special characters of their own. The render size on tag token should never
166  /// never match the text size, instead being 0 or 1.
167  ///////////////////////////////////////
168  class MEZZ_LIB TagToken : public TextToken
169  {
170  protected:
171  friend class MarkupParser;
172  /// @brief Unconverted version of the tag name.
174  /// @brief The parameters provided for this tag, if any.
176  public:
177  /// @brief Class constructor.
179  { }
180  /// @brief Descriptive constructor.
181  /// @param RawText A string containing the raw text of this token.
182  /// @param Name The name of this tag.
183  /// @param TType The type of text token to be created.
184  TagToken(const String& RawText, const String& Name, const TokenType TType) :
185  TextToken(RawText,TType), TagName(Name) { }
186  /// @brief Class destructor.
187  virtual ~TagToken()
188  { }
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  // Utility
192 
193  /// @brief Gets the name of the tag this token represents.
194  /// @return Returns a reference to a string containing the name of this tokens tag.
195  const String& GetTagName() const;
196  /// @brief Gets a parameter specified in this token by name.
197  /// @param Param The name of the parameter to retrieve.
198  /// @return Returns a string containing the value specified in this token.
199  String GetParameter(const String& Param) const;
200  };//TagToken
201 
202  ///////////////////////////////////////////////////////////////////////////////
203  /// @brief This struct represents a markup tag segment from the source string.
204  /// @details Tag tokens are tokens that represent text that isn't intended to be rendered, but instead alter
205  /// other text around it or create special characters of their own. The render size on tag token should never
206  /// never match the text size, instead being 0 or 1.
207  ///////////////////////////////////////
209  {
210  protected:
211  friend class MarkupParser;
212  /// @brief Pointer to the opening/closing tag for this tag.
214  public:
215  /// @brief Class constructor.
217  PartnerTag(NULL) { }
218  /// @brief Descriptive constructor.
219  /// @param RawText A string containing the raw text of this token.
220  /// @param Name The name of this tag.
221  /// @param TType The type of text token to be created.
222  RangeTagToken(const String& RawText, const String& Name, const TokenType TType) :
223  TagToken(RawText,Name,TType), PartnerTag(NULL) { }
224  /// @brief Class destructor.
225  virtual ~RangeTagToken()
226  { }
227 
228  ///////////////////////////////////////////////////////////////////////////////
229  // Utility
230 
231  /// @brief Gets the partner tag to this RangeTag
232  /// @return Returns a pointer to the other tag paired with this range tag.
233  RangeTagToken* GetPartnerTag() const;
234 
235  ///////////////////////////////////////////////////////////////////////////////
236  // Inserting and Removing
237 
238  /// @copydoc TextToken::InsertCharacter(const UInt32, UInt32)
239  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
240  /// @copydoc TextToken::InsertCharacters(const UInt32, Char8*, const UInt32)
241  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
242  /// @copydoc TextToken::InsertCharacters(const UInt32, const UInt32*, const UInt32)
243  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
244  /// @copydoc TextToken::RemoveCharacter(const UInt32)
245  virtual UInt32 RemoveCharacter(const UInt32 Index);
246  /// @copydoc TextToken::RemoveCharacters(const UInt32, const UInt32)
247  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
248  /// @copydoc TextToken::ClearAllCharacters()
249  virtual UInt32 ClearAllCharacters();
250  };//RangeTagToken
251 
252  ///////////////////////////////////////////////////////////////////////////////
253  /// @brief This struct represents a markup tag segment from the source string.
254  /// @details Tag tokens are tokens that represent text that isn't intended to be rendered, but instead alter
255  /// other text around it or create special characters of their own. The render size on tag token should never
256  /// never match the text size, instead being 0 or 1.
257  ///////////////////////////////////////
259  {
260  protected:
261  friend class MarkupParser;
262  public:
263  /// @brief Class constructor.
265  { }
266  /// @brief Descriptive constructor.
267  /// @param RawText A string containing the raw text of this token.
268  /// @param Name The name of this tag.
269  /// @param TType The type of text token to be created.
270  InsertTagToken(const String& RawText, const String& Name) :
271  TagToken(RawText,Name,TextToken::TT_InsertTag) { }
272  /// @brief Class destructor.
273  virtual ~InsertTagToken()
274  { }
275 
276  ///////////////////////////////////////////////////////////////////////////////
277  // Inserting and Removing
278 
279  /// @copydoc TextToken::InsertCharacter(const UInt32, UInt32)
280  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
281  /// @copydoc TextToken::InsertCharacters(const UInt32, Char8*, const UInt32)
282  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
283  /// @copydoc TextToken::InsertCharacters(const UInt32, const UInt32*, const UInt32)
284  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
285  /// @copydoc TextToken::RemoveCharacter(const UInt32)
286  virtual UInt32 RemoveCharacter(const UInt32 Index);
287  /// @copydoc TextToken::RemoveCharacters(const UInt32, const UInt32)
288  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
289  /// @copydoc TextToken::ClearAllCharacters()
290  virtual UInt32 ClearAllCharacters();
291  };//InsertTagToken
292 
293  ///////////////////////////////////////////////////////////////////////////////
294  /// @brief This is a helper class that facilitates operations with collections of tokens generated from Markup Parsers.
295  /// @details
296  ///////////////////////////////////////
298  {
299  public:
300  /// @brief Basic container type for the storage of @ref TextToken instances by this class.
301  typedef std::vector<TextToken*> TokenContainer;
302  /// @brief Iterator type for @ref TextToken instances being stored by this class.
303  typedef TokenContainer::iterator TokenIterator;
304  /// @brief Const Iterator type for @ref TextToken instances being stored by this class.
305  typedef TokenContainer::const_iterator ConstTokenIterator;
306  /// @brief Reverse Iterator type for @ref TextToken instances being stored by this class.
307  typedef TokenContainer::reverse_iterator ReverseTokenIterator;
308  /// @brief Const Reverse Iterator type for @ref TextToken instances being stored by this class.
309  typedef TokenContainer::const_reverse_iterator ConstReverseTokenIterator;
310  /// @brief An std::pair used to report the result of a TextToken and it's local index.
311  typedef std::pair<TokenIterator,UInt32> TokenIndexPair;
312  protected:
313  /// @internal
314  /// @brief Container for TextToken storage.
316  /// @internal
317  /// @brief Gets the token at the string index, and it's local index.
318  TokenIndexPair GetTokenIndex(const UInt32 Index);
319  public:
320  /// @brief Class constructor.
321  TokenString();
322  /// @brief Class destructor.
323  virtual ~TokenString();
324 
325  ///////////////////////////////////////////////////////////////////////////////
326  // Utility
327 
328  /// @brief Gets a string containing all the raw characters of the tokens in this string.
329  /// @return Returns a string of all the UTF-8 characters in the tokens of this string.
330  virtual String GetRawString() const;
331 
332  /// @brief Gets an iterator to the first TextToken.
333  /// @return Returns an iterator to the first TextToken being stored by this TokenString.
334  TokenIterator BeginToken();
335  /// @brief Gets an iterator to one passed the last TextToken.
336  /// @return Returns an iterator to one passed the last TextToken being stored by this TokenString.
337  TokenIterator EndToken();
338  /// @brief Gets a const iterator to the first TextToken.
339  /// @return Returns a const iterator to the first TextToken being stored by this TokenString.
340  ConstTokenIterator BeginToken() const;
341  /// @brief Gets a const iterator to one passed the last TextToken.
342  /// @return Returns a const iterator to one passed the last TextToken being stored by this TokenString.
343  ConstTokenIterator EndToken() const;
344 
345  /// @brief Gets a reverse iterator to the last TextToken.
346  /// @return Returns a reverse iterator to the last TextToken being stored by this TokenString.
347  ReverseTokenIterator ReverseBeginToken();
348  /// @brief Gets a reverse iterator to one before the first TextToken.
349  /// @return Returns a reverse iterator to one before the first TextToken being stored by this TokenString.
350  ReverseTokenIterator ReverseEndToken();
351  /// @brief Gets a const reverse iterator to the last TextToken.
352  /// @return Returns a const reverse iterator to the last TextToken being stored by this TokenString.
353  ConstReverseTokenIterator ReverseBeginToken() const;
354  /// @brief Gets a const reverse iterator to one before the first TextToken.
355  /// @return Returns a const reverse iterator to one before the first TextToken being stored by this TokenString.
356  ConstReverseTokenIterator ReverseEndToken() const;
357 
358  ///////////////////////////////////////////////////////////////////////////////
359  // Generating and Destroying the String
360 
361  /// @brief Appends a new token to the end of this string.
362  /// @note Tokens appended to this string will become owned by this string and deleted when this string is destructed.
363  /// @param ToBePushed The token being added to this string.
364  virtual void PushToken(TextToken* ToBePushed);
365  /// @brief Appends a group of tokens to the end of this string.
366  /// @note Tokens appended to this string will become owned by this string and deleted when this string is destructed.
367  /// @param ToBePushed A container
368  virtual void PushTokens(const TokenContainer& ToBePushed);
369  /// @brief Destroys all tokens currently in this string.
370  virtual void DestroyAllTokens();
371 
372  ///////////////////////////////////////////////////////////////////////////////
373  // Inserting and Removing
374 
375  /// @brief Inserts a single UTF-32 size character into this string.
376  /// @param Index The index at which the character will be inserted.
377  /// @param UChar The unicode character to be inserted. This will be converted to UTF-8 prior to insertion.
378  /// @return Returns 1 if the character was successfully inserted, otherwise returns 0.
379  virtual UInt32 InsertCharacter(const UInt32 Index, UInt32 UChar);
380  /// @brief Inserts multiple characters into this string.
381  /// @param Index The index at which the characters will be inserted.
382  /// @param Characters An array of Char8's encoded in UTF-8 to be inserted.
383  /// @param Size The size of the array of Char8's passed in.
384  /// @return Returns the number of characters successfully inserted.
385  virtual UInt32 InsertCharacters(const UInt32 Index, const Char8* Characters, const UInt32 Size);
386  /// @brief Inserts multiple characters into this string.
387  /// @param Index The index at which the characters will be inserted.
388  /// @param Characters An array of UInt32's encoded in UTF-32 to be inserted.
389  /// @param Size The size of the array of UInt32's passed in.
390  /// @return Returns the number of characters successfully inserted.
391  virtual UInt32 InsertCharacters(const UInt32 Index, const UInt32* Characters, const UInt32 Size);
392  /// @brief Removes a single rendered character from this string.
393  /// @param Index The index at which the rendered character will be removed.
394  /// @return Returns 1 if the character was successfully removed, otherwise returns 0.
395  virtual UInt32 RemoveCharacter(const UInt32 Index);
396  /// @brief Removes rendered characters from this string.
397  /// @param Index The index at which to start removing characters from this string.
398  /// @param Length The number of characters to try and remove from this string.
399  /// @return Returns the number of rendered characters that were actually removed.
400  virtual UInt32 RemoveCharacters(const UInt32 Index, const UInt32 Length);
401  /// @brief Removes all the rendered characters from this string.
402  /// @return Returns the number of rendered characters that were actually removed.
403  virtual UInt32 ClearAllCharacters();
404  };//InsertTagToken
405  }//UI
406 }//Mezzanine
407 
408 #endif