MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
glyph.h
1 // © Copyright 2010 - 2014 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 _uiglyph_h
41 #define _uiglyph_h
42 
43 #include "vector2.h"
44 #include "UI/uienumerations.h"
45 #include "UI/kerning.h"
46 #include "UI/font.h"
47 #include "UI/textureatlas.h"
48 #include "exception.h"
49 #include <vector>
50 
51 namespace Mezzanine
52 {
53  namespace UI
54  {
55  ///////////////////////////////////////////////////////////////////////////////
56  /// @brief Class used to describe a single glyph or character available for text operations.
57  /// @details
58  ///////////////////////////////////////
60  {
61  public:
62  typedef std::vector<KerningInfo> KerningContainer;
63  typedef KerningContainer::iterator KerningIterator;
64  typedef KerningContainer::const_iterator ConstKerningIterator;
65 
66  /// @enum Whitespace
67  /// @brief This enum represents the common whitespace characters found in Ascii/UTF-8.
69  {
70  HT = 0x0009, ///< Horizontal Tab
71  LF = 0x000A, ///< Line Feed/Newline
72  VT = 0x000B, ///< Vertical Tab
73  CR = 0x000D, ///< Carriage Return
74  Space = 0x0020, ///< Space
75  // NEL is a bit hazy, care to be taken until the UI system is completely converted to Unicode
76  NEL = 0x0085 ///< Next Line/Newline
77  };
78  public:
79  /// @brief Default constructor.
80  /// @param Data The collection of glyphs this glyph belongs to.
81  Glyph(FontData* Data) :
82  Atlas(Data->GetAtlas()), Font(Data), GlyphID(0), GlyphAdvance(0), VerticalOffset(0) { }
83  /// @brief Descriptive constructor.
84  /// @param Data The collection of glyphs this glyph belongs to.
85  /// @param TexAtlas The TextureAtlas this glyph belongs to.
86  /// @param ID The Character this glyph information represents.
87  /// @param Advance The number of pixels to advance the cursor for the next glyph.
88  /// @param VertOffset The amount of pixels the glyph is to be adjusted on the Y axis.
89  Glyph(FontData* Data, TextureAtlas* TexAtlas, const UInt32& ID, const Real& Advance, const Real& VertOffset) :
90  Atlas(TexAtlas), Font(Data), GlyphID(ID), GlyphAdvance(Advance), VerticalOffset(VertOffset) { }
91  /// @brief Class destructor.
93  { this->Kernings.clear(); }
94 
95  ///////////////////////////////////////////////////////////////////////////////
96  // Public Data Members
97 
98  /// @brief The TextureAtlas this glyph belongs to.
100  /// @brief The collection of glyphs this glyph belongs to.
102  /// @brief The Character this glyph information represents.
104  /// @brief The number of pixels to advance the cursor for the next glyph.
106  /// @brief The amount of pixels the glyph is to be adjusted on the Y axis.
108  /// @brief The 4 corner coordinates on the Texture.
109  Vector2 AtlasCoords[4];
110  /// @brief List of all the Kernings that apply to this glyph.
111  KerningContainer Kernings;
112 
113  ///////////////////////////////////////////////////////////////////////////////
114  // Utility Methods
115 
116  /// @brief Gets the name of the atlas this glyph belongs to.
117  /// @return Returns a string containing the name of the atlas this glyph belongs to.
118  inline const String& GetAtlasName() const
119  { return this->Atlas->GetName(); }
120  /// @brief Convenience function for getting the Kerning information for a given Glyph.
121  /// @note This is the character to the left in languages like English(left to right), or the character to the right in languages like Arabic(right to left).
122  /// @param Previous The previous glyph in the sequence.
123  /// @return Returns in pixels the special amount to advance.
124  inline Real GetKerning(const UInt32 Previous) const
125  {
126  Whole NumKernings = this->Kernings.size();
127  if( NumKernings == 0 || Previous == 0 )
128  return 0;
129 
130  for( Whole Index = 0 ; Index < NumKernings ; ++Index )
131  {
132  if( this->Kernings[Index].Character == Previous )
133  return this->Kernings[Index].Kerning;
134  }
135  return 0;
136  }
137 
138  ///////////////////////////////////////////////////////////////////////////////
139  // Whitespace Checks
140 
141  /// @brief Checks if this glyph is a horizontal tab.
142  inline bool IsHorizontalTab() const
143  { return (this->GlyphID == HT); }
144  /// @brief Checks if this glyph is a line feed.
145  inline bool IsLineFeed() const
146  { return (this->GlyphID == LF); }
147  /// @brief Checks if this glyph is a vertical tab.
148  inline bool IsVerticalTab() const
149  { return (this->GlyphID == VT); }
150  /// @brief Checks if this glyph is a carriage return.
151  inline bool IsCarriageReturn() const
152  { return (this->GlyphID == CR); }
153  /// @brief Checks if this glyph is a space.
154  inline bool IsSpace() const
155  { return (this->GlyphID == Space); }
156  /// @brief Checks if this glyph is a next line.
157  inline bool IsNextLine() const
158  { return (this->GlyphID == NEL); }
159 
160  /// @brief Checks if this glyph marks a tab.
161  inline bool IsTab() const
162  { return this->IsHorizontalTab() || this->IsVerticalTab(); }
163  /// @brief Checks if this glyph marks a new line.
164  inline bool IsNewLine() const
165  { return this->IsLineFeed() || this->IsCarriageReturn() || this->IsNextLine(); }
166  /// @brief Checks if this glyph is not renderable.
167  inline bool IsWhitespace() const
168  { return this->IsTab() || this->IsNewLine() || this->IsSpace(); }
169 
170  ///////////////////////////////////////////////////////////////////////////////
171  // Position and Size Methods
172 
173  /// @brief Gets the Top coordinate on the Texture.
174  inline Real GetUVTop() const
175  { return this->AtlasCoords[UI::QC_TopLeft].Y * this->Atlas->GetTextureSize().Y; }
176  /// @brief Gets the Bottom coordinate on the Texture.
177  inline Real GetUVBottom() const
178  { return this->AtlasCoords[UI::QC_BottomRight].Y * this->Atlas->GetTextureSize().Y; }
179  /// @brief Gets the Left coordinate on the Texture.
180  inline Real GetUVLeft() const
181  { return this->AtlasCoords[UI::QC_TopLeft].X * this->Atlas->GetTextureSize().X; }
182  /// @brief Gets the Right coordinate on the Texture.
183  inline Real GetUVRight() const
184  { return this->AtlasCoords[UI::QC_BottomRight].X * this->Atlas->GetTextureSize().X; }
185  /// @brief Gets the glyphs height on the Texture.
186  inline Real GetHeight() const
187  { return this->GetSize().Y; }
188  /// @brief Gets the glyphs width on the Texture.
189  inline Real GetWidth() const
190  { return this->GetSize().X; }
191  /// @brief Gets the position of the glyph on the Texture
192  inline Vector2 GetPosition() const
193  { return this->AtlasCoords[UI::QC_TopLeft] * this->Atlas->GetTextureSize(); }
194  /// @brief Gets the size of the glyph on the Texture.
195  inline Vector2 GetSize() const
196  {
197  if( this->IsNewLine() ) return Vector2(0,0);
198  else if( this->IsSpace() || this->IsTab() ) return Vector2(this->GlyphAdvance,this->Font->GetBaseLine());
199  else return (this->AtlasCoords[UI::QC_BottomRight] - this->AtlasCoords[UI::QC_TopLeft]) * this->Atlas->GetTextureSize();
200  }
201  /// @brief Gets the pixel position on the Atlas of a corner belonging to this glyph.
202  /// @param Corner The corner to retrieve the coordinates for.
203  /// @return Returns a Vector2 containing the Atlas pixel position of the specific corner.
204  inline Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
205  {
206  switch(Corner)
207  {
208  case UI::QC_TopLeft: return this->AtlasCoords[UI::QC_TopLeft] * this->Atlas->GetTextureSize(); break;
209  case UI::QC_TopRight: return this->AtlasCoords[UI::QC_TopRight] * this->Atlas->GetTextureSize(); break;
210  case UI::QC_BottomLeft: return this->AtlasCoords[UI::QC_BottomLeft] * this->Atlas->GetTextureSize(); break;
211  case UI::QC_BottomRight: return this->AtlasCoords[UI::QC_BottomRight] * this->Atlas->GetTextureSize(); break;
212  }
213  return Vector2(0,0);
214  }
215  /// @brief Gets the relative position on the Atlas of a corner belonging to this glyph.
216  /// @param Corner The corner to retrieve the coordinates for.
217  /// @return Returns a Vector2 containing the Atlas relative position of the specific corner.
218  inline Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
219  {
220  switch(Corner)
221  {
222  case UI::QC_TopLeft: return this->AtlasCoords[UI::QC_TopLeft]; break;
223  case UI::QC_TopRight: return this->AtlasCoords[UI::QC_TopRight]; break;
224  case UI::QC_BottomLeft: return this->AtlasCoords[UI::QC_BottomLeft]; break;
225  case UI::QC_BottomRight: return this->AtlasCoords[UI::QC_BottomRight]; break;
226  }
227  return Vector2(0,0);
228  }
229  };//Glyph
230  }//UI
231 }//Mezzanine
232 
233 #endif