Spinning Topp Logo BlackTopp Studios
inc
glyph.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 _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  /// @brief Container type for Kerning information stored by this class.
63  typedef std::vector<KerningInfo> KerningContainer;
64  /// @brief Iterator type for Kerning information stored in this class.
65  typedef KerningContainer::iterator KerningIterator;
66  /// @brief Const Iterator type for Kerning information stored in this class.
67  typedef KerningContainer::const_iterator ConstKerningIterator;
68 
69  /// @brief This enum represents the common whitespace characters found in Ascii/UTF-8.
71  {
72  HT = 0x0009, ///< Horizontal Tab
73  LF = 0x000A, ///< Line Feed/Newline
74  VT = 0x000B, ///< Vertical Tab
75  CR = 0x000D, ///< Carriage Return
76  Space = 0x0020, ///< Space
77  // NEL is a bit hazy, care to be taken until the UI system is completely converted to Unicode
78  NEL = 0x0085 ///< Next Line/Newline
79  };
80  public:
81  /// @brief Default constructor.
82  /// @param Data The collection of glyphs this glyph belongs to.
83  Glyph(FontData* Data) :
84  Atlas(Data->GetAtlas()), Font(Data), GlyphID(0), GlyphAdvance(0), VerticalOffset(0) { }
85  /// @brief Descriptive constructor.
86  /// @param Data The collection of glyphs this glyph belongs to.
87  /// @param TexAtlas The TextureAtlas this glyph belongs to.
88  /// @param ID The Character this glyph information represents.
89  /// @param Advance The number of pixels to advance the cursor for the next glyph.
90  /// @param VertOffset The amount of pixels the glyph is to be adjusted on the Y axis.
91  Glyph(FontData* Data, TextureAtlas* TexAtlas, const UInt32& ID, const Real& Advance, const Real& VertOffset) :
92  Atlas(TexAtlas), Font(Data), GlyphID(ID), GlyphAdvance(Advance), VerticalOffset(VertOffset) { }
93  /// @brief Class destructor.
95  { this->Kernings.clear(); }
96 
97  ///////////////////////////////////////////////////////////////////////////////
98  // Public Data Members
99 
100  /// @brief The TextureAtlas this glyph belongs to.
102  /// @brief The collection of glyphs this glyph belongs to.
104  /// @brief The Character this glyph information represents.
106  /// @brief The number of pixels to advance the cursor for the next glyph.
108  /// @brief The amount of pixels the glyph is to be adjusted on the Y axis.
110  /// @brief The 4 corner coordinates on the Texture.
111  Vector2 AtlasCoords[4];
112  /// @brief List of all the Kernings that apply to this glyph.
113  KerningContainer Kernings;
114 
115  ///////////////////////////////////////////////////////////////////////////////
116  // Utility Methods
117 
118  /// @brief Gets the name of the atlas this glyph belongs to.
119  /// @return Returns a string containing the name of the atlas this glyph belongs to.
120  inline const String& GetAtlasName() const
121  { return this->Atlas->GetName(); }
122  /// @brief Convenience function for getting the Kerning information for a given Glyph.
123  /// @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).
124  /// @param Previous The previous glyph in the sequence.
125  /// @return Returns in pixels the special amount to advance.
126  inline Real GetKerning(const UInt32 Previous) const
127  {
128  Whole NumKernings = this->Kernings.size();
129  if( NumKernings == 0 || Previous == 0 )
130  return 0;
131 
132  for( Whole Index = 0 ; Index < NumKernings ; ++Index )
133  {
134  if( this->Kernings[Index].Character == Previous )
135  return this->Kernings[Index].Kerning;
136  }
137  return 0;
138  }
139 
140  ///////////////////////////////////////////////////////////////////////////////
141  // Whitespace Checks
142 
143  /// @brief Checks if this glyph is a horizontal tab.
144  inline bool IsHorizontalTab() const
145  { return (this->GlyphID == HT); }
146  /// @brief Checks if this glyph is a line feed.
147  inline bool IsLineFeed() const
148  { return (this->GlyphID == LF); }
149  /// @brief Checks if this glyph is a vertical tab.
150  inline bool IsVerticalTab() const
151  { return (this->GlyphID == VT); }
152  /// @brief Checks if this glyph is a carriage return.
153  inline bool IsCarriageReturn() const
154  { return (this->GlyphID == CR); }
155  /// @brief Checks if this glyph is a space.
156  inline bool IsSpace() const
157  { return (this->GlyphID == Space); }
158  /// @brief Checks if this glyph is a next line.
159  inline bool IsNextLine() const
160  { return (this->GlyphID == NEL); }
161 
162  /// @brief Checks if this glyph marks a tab.
163  inline bool IsTab() const
164  { return this->IsHorizontalTab() || this->IsVerticalTab(); }
165  /// @brief Checks if this glyph marks a new line.
166  inline bool IsNewLine() const
167  { return this->IsLineFeed() || this->IsCarriageReturn() || this->IsNextLine(); }
168  /// @brief Checks if this glyph is not renderable.
169  inline bool IsWhitespace() const
170  { return this->IsTab() || this->IsNewLine() || this->IsSpace(); }
171 
172  ///////////////////////////////////////////////////////////////////////////////
173  // Position and Size Methods
174 
175  /// @brief Gets the Top coordinate on the Texture.
176  inline Real GetUVTop() const
177  { return this->AtlasCoords[UI::QC_TopLeft].Y * this->Atlas->GetTextureSize().Y; }
178  /// @brief Gets the Bottom coordinate on the Texture.
179  inline Real GetUVBottom() const
180  { return this->AtlasCoords[UI::QC_BottomRight].Y * this->Atlas->GetTextureSize().Y; }
181  /// @brief Gets the Left coordinate on the Texture.
182  inline Real GetUVLeft() const
183  { return this->AtlasCoords[UI::QC_TopLeft].X * this->Atlas->GetTextureSize().X; }
184  /// @brief Gets the Right coordinate on the Texture.
185  inline Real GetUVRight() const
186  { return this->AtlasCoords[UI::QC_BottomRight].X * this->Atlas->GetTextureSize().X; }
187  /// @brief Gets the glyphs height on the Texture.
188  inline Real GetHeight() const
189  { return this->GetSize().Y; }
190  /// @brief Gets the glyphs width on the Texture.
191  inline Real GetWidth() const
192  { return this->GetSize().X; }
193  /// @brief Gets the position of the glyph on the Texture
194  inline Vector2 GetPosition() const
195  { return this->AtlasCoords[UI::QC_TopLeft] * this->Atlas->GetTextureSize(); }
196  /// @brief Gets the size of the glyph on the Texture.
197  inline Vector2 GetSize() const
198  {
199  if( this->IsNewLine() ) return Vector2(0,0);
200  else if( this->IsSpace() || this->IsTab() ) return Vector2(this->GlyphAdvance,this->Font->GetBaseLine());
201  else return (this->AtlasCoords[UI::QC_BottomRight] - this->AtlasCoords[UI::QC_TopLeft]) * this->Atlas->GetTextureSize();
202  }
203  /// @brief Gets the pixel position on the Atlas of a corner belonging to this glyph.
204  /// @param Corner The corner to retrieve the coordinates for.
205  /// @return Returns a Vector2 containing the Atlas pixel position of the specific corner.
206  inline Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
207  {
208  switch(Corner)
209  {
210  case UI::QC_TopLeft: return this->AtlasCoords[UI::QC_TopLeft] * this->Atlas->GetTextureSize(); break;
211  case UI::QC_TopRight: return this->AtlasCoords[UI::QC_TopRight] * this->Atlas->GetTextureSize(); break;
212  case UI::QC_BottomLeft: return this->AtlasCoords[UI::QC_BottomLeft] * this->Atlas->GetTextureSize(); break;
213  case UI::QC_BottomRight: return this->AtlasCoords[UI::QC_BottomRight] * this->Atlas->GetTextureSize(); break;
214  }
215  return Vector2(0,0);
216  }
217  /// @brief Gets the relative position on the Atlas of a corner belonging to this glyph.
218  /// @param Corner The corner to retrieve the coordinates for.
219  /// @return Returns a Vector2 containing the Atlas relative position of the specific corner.
220  inline Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
221  {
222  switch(Corner)
223  {
224  case UI::QC_TopLeft: return this->AtlasCoords[UI::QC_TopLeft]; break;
225  case UI::QC_TopRight: return this->AtlasCoords[UI::QC_TopRight]; break;
226  case UI::QC_BottomLeft: return this->AtlasCoords[UI::QC_BottomLeft]; break;
227  case UI::QC_BottomRight: return this->AtlasCoords[UI::QC_BottomRight]; break;
228  }
229  return Vector2(0,0);
230  }
231  };//Glyph
232  }//UI
233 }//Mezzanine
234 
235 #endif
~Glyph()
Class destructor.
Definition: glyph.h:94
This class represents a collection of Glyphs in a common visual style.
Definition: font.h:55
Vector2 GetTextureSize() const
Gets the size of the TextureAtlas.
bool IsWhitespace() const
Checks if this glyph is not renderable.
Definition: glyph.h:169
Real GetUVBottom() const
Gets the Bottom coordinate on the Texture.
Definition: glyph.h:179
const String & GetName() const
Gets the name of this Texture Atlas.
const String & GetAtlasName() const
Gets the name of the atlas this glyph belongs to.
Definition: glyph.h:120
Class used to describe a single glyph or character available for text operations. ...
Definition: glyph.h:59
Real GetUVRight() const
Gets the Right coordinate on the Texture.
Definition: glyph.h:185
KerningContainer Kernings
List of all the Kernings that apply to this glyph.
Definition: glyph.h:113
Real GetWidth() const
Gets the glyphs width on the Texture.
Definition: glyph.h:191
bool IsNextLine() const
Checks if this glyph is a next line.
Definition: glyph.h:159
Vector2 GetSize() const
Gets the size of the glyph on the Texture.
Definition: glyph.h:197
QuadCorner
Used by Sprites and Glyphs for tracking their placement on a TextureAtlas.
Whitespace
This enum represents the common whitespace characters found in Ascii/UTF-8.
Definition: glyph.h:70
This is a collection of smaller textures packed into a larger texture, intended to increase UI perfor...
Definition: textureatlas.h:71
This implements the exception hiearchy for Mezzanine.
bool IsVerticalTab() const
Checks if this glyph is a vertical tab.
Definition: glyph.h:150
KerningContainer::iterator KerningIterator
Iterator type for Kerning information stored in this class.
Definition: glyph.h:65
Real VerticalOffset
The amount of pixels the glyph is to be adjusted on the Y axis.
Definition: glyph.h:109
Real GlyphAdvance
The number of pixels to advance the cursor for the next glyph.
Definition: glyph.h:107
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
Glyph(FontData *Data)
Default constructor.
Definition: glyph.h:83
std::vector< KerningInfo > KerningContainer
Container type for Kerning information stored by this class.
Definition: glyph.h:63
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
bool IsNewLine() const
Checks if this glyph marks a new line.
Definition: glyph.h:166
FontData * Font
The collection of glyphs this glyph belongs to.
Definition: glyph.h:103
This class creates and encapsultes a character that can be used in text renders.
Definition: character.h:59
Real X
Coordinate on the X vector.
Definition: vector2.h:67
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
bool IsTab() const
Checks if this glyph marks a tab.
Definition: glyph.h:163
Real GetUVLeft() const
Gets the Left coordinate on the Texture.
Definition: glyph.h:182
Real GetHeight() const
Gets the glyphs height on the Texture.
Definition: glyph.h:188
TextureAtlas * Atlas
The TextureAtlas this glyph belongs to.
Definition: glyph.h:101
Real GetKerning(const UInt32 Previous) const
Convenience function for getting the Kerning information for a given Glyph.
Definition: glyph.h:126
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
bool IsCarriageReturn() const
Checks if this glyph is a carriage return.
Definition: glyph.h:153
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Real GetBaseLine() const
Gets the height of the largest glyph in this font.
Definition: font.cpp:85
bool IsHorizontalTab() const
Checks if this glyph is a horizontal tab.
Definition: glyph.h:144
bool IsSpace() const
Checks if this glyph is a space.
Definition: glyph.h:156
bool IsLineFeed() const
Checks if this glyph is a line feed.
Definition: glyph.h:147
Vector2 GetRelativeAtlasCoords(const UI::QuadCorner Corner) const
Gets the relative position on the Atlas of a corner belonging to this glyph.
Definition: glyph.h:220
Vector2 GetPosition() const
Gets the position of the glyph on the Texture.
Definition: glyph.h:194
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
UInt32 GlyphID
The Character this glyph information represents.
Definition: glyph.h:105
KerningContainer::const_iterator ConstKerningIterator
Const Iterator type for Kerning information stored in this class.
Definition: glyph.h:67
Glyph(FontData *Data, TextureAtlas *TexAtlas, const UInt32 &ID, const Real &Advance, const Real &VertOffset)
Descriptive constructor.
Definition: glyph.h:91
Vector2 GetAtlasCoords(const UI::QuadCorner Corner) const
Gets the pixel position on the Atlas of a corner belonging to this glyph.
Definition: glyph.h:206
Real GetUVTop() const
Gets the Top coordinate on the Texture.
Definition: glyph.h:176